'In Xamarin, pass the received Intent from another app between different activities in the same app
I have two actions in the Xamarin App (Splash Screen Activity: com.example.splashactivity, and Main Activity: com.example.mainactivity).
In a normal scenario: the splash screen appears when the app initially launches, displaying a short animation before redirecting to the main activity.
SplashActivity.cs
[Activity(Name = "com.example.splashactivity")]
public class SplashActivity : AppCompatActivity, Animator.IAnimatorListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Code
// Redirects to MainActivity
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
But when an intent is sent from another app (let's call it Second App), the main activity is immediately opened without any animation (obviously, com.example.mainactivity is called).
Now, when I put the splash activity's name (com.example.splashactivity) into the Second App, it launches the splash screen and then redirects to the main activity. I keep the Intent Values in SecureStorage since I require them throughout the app.
When I was done with the operation, I sent a message (MessagingCenter.Send<object>()) to Splash Activity and gave it data to send back in Intent Extra, but it remained stuck there (no redirection to Second App)
[Activity(Name = "com.example.splashactivity")]
public class SplashActivity : AppCompatActivity, Animator.IAnimatorListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Receive Intent from Second App
string id = Intent.GetStringExtra("id");
if (Intent.HasExtra("id"))
{
await SecureStorage.SetAsync("Intent_id", id);
}
// Sends Intent Extra Back to Second App
MessagingCenter.Subscribe<object>(this, "IntentBack", async (e) =>
{
var msg = await SecureStorage.GetAsync("IntentBack_msg");
Intent intent = new Intent(Intent.ActionMain);
intent.PutExtra("msg", msg);
SetResult(Result.Ok, intent);
Finish();
});
// Redirects to MainActivity
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
