in MainActivity.cs I send the following message:
resultIntent.PutExtra("message", "string123");
then in App.css i am trying to accept the message as follows:
Intent.Extras.GetString("message", "");
but the path I'm using is for MainActivity.cs...
CodePudding user response:
It seems like you want to pass a message from MainActivity to App when the App constructor is first created. This you can achieve in a number of ways
Option 1 (Recommended)
Simply by giving a parameter to App constructor as follows
public App(string message)
{
InitializeComponent();
MainPage = new AppShell(message);
}
This you can set from MainActivity on creation as follows
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App("string123"));
}
Option 2
Yet another option, which i would not prefer, could be to pass an instance of MainActivity to App as follows
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.Intent.PutExtra("message", "string1234");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App(this));
}
then in App constructor you use this instance as follows
Android.App.Activity _mainActivity;
public App(object mainActivity)
{
InitializeComponent();
string message = "Void";
if (Device.RuntimePlatform == Device.Android)
{
_mainActivity = (Android.App.Activity)mainActivity;
message = _mainActivity.Intent.Extras.GetString("message", "");
}
MainPage = new AppShell(message);
}