I have a problem with Android and the transitions with activities. What I want to have is the following:
What I tried:
Main Activity:
android: launch mode = "single task"
Start Activity B:
Intent intent = new Intent (this, B.class);
this.startActivity (intent);
Activity B:
android: launch mode = "SingleInstance"
Start MainActivity:
Intent callerIntent = new Intent (this, MainActivity.class);
CallerIntent.addFlags (Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
start activity (caller intent);
Unfortunately point 5 does not work.
I have searched for a long time, but unfortunately found nothing or searched for the bottle words. I hope somebody can help me.
I also have a small demo app on which you can test the behavior: AndroidStudioZipFile (30 days available)
Do not use special launch modes for this!
MainActivity
should call ActivityB
like this:
Intent intent = new Intent(this. ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
ActivityB
should call MainActivity
like this:
Intent intent = new Intent(this. MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
This will allow you to flip back and forth between the 2 activities, without creating any new instances. You will need to override onBackPressed()
in one or both activities.
If you want the BACK button in MainActivity
to exit the app, you will need to be a bit tricky. An easy way to do this is as follows:
Declare a static variable in MainActivity
like this:
public static boolean exit = false;
in MainActivity.onBackPressed()
, do the following:
exit = true;
super.onBackPressed();
This will cause MainActivity
to finish. If MainActivity
is the only active Activity
, then you are done. However, it is possible that ActivityB
is underneath MainActivity
. To force ActivityB
to finish in this situation, add this to ActivityB.onResume()
:
super.onResume();
if (MainActivity.exit) {
finish();
return;
}
// Rest of onResume() code goes here...