2

I have a problem with Android and the transitions with activities. What I want to have is the following:

  1. MainActivity calls Activity B.
  2. Activity B calls MainActivity (for example, via Back Button, same instance)
  3. MainActivity calls the same instance of Activity B. (Same instance is important because Activity B takes a long time to start)
  4. Activity B calls MainActivity (for example, via Back Button, same instance)
  5. MainActivity Back Button is pressed and app is terminated.

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)

David Wasser
  • 93,459
  • 16
  • 209
  • 274
user3215952
  • 223
  • 5
  • 11

2 Answers2

0

What is the behavior in Step 5? I think, it will be showing Activity B after step-5 which is still in the backstack since is it never destroyed or finished! if you do not want to save anything on back press of MainActivity, can you call

@Override
public void onBackPressed() {
    this.finish();
    System.exit(0);
}

or you may also try to clear the back stack when back is pressed on MainActivity! Hope this helps.

Swati
  • 1,179
  • 9
  • 28
  • The behavior in Step 5 is, the activity called again. In the MainActivity (ReactNativePart) runs a WebView, where you can browse back to the last visited websites. So the whole story is, I can browse inside an webview (created with react native) and on some sites, I can start a native activity. I have to check if that is possible to override this method inside react native. The existing back functionality inside the webview still needs to go. – user3215952 Aug 17 '17 at 20:26
  • This solution doesn't work as I want. I had a solution which I call the home screen at onBackPressed and that works but it isn't so beautiful. – user3215952 Aug 21 '17 at 11:58
0

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...
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Hi David Thank you for your solution. The Intent.FLAG_ACTIVITY_REORDER_TO_FRONT works perfect, but the exit boolean flag doesn't work. The problem is, when I finish the MainActivity and reopen it again, the Activity B start. I found a better solution for that. I call finishAffinity() onBackPressed() without calling the super-method. This close also ActivityB. – user3215952 Aug 21 '17 at 11:49
  • Using `finishAffinity()` works, but is only available from API level 16. If you want to support older devices you need another mechanism. I see no reason why `ActivityB` should start when you reopen your app. Did you start your app by launching `ActivityB`?? – David Wasser Aug 21 '17 at 12:39
  • Anyway, glad you have a solution that works, in any case! – David Wasser Aug 21 '17 at 12:40
  • The app android compatibility is 4.1 and greater, so it's fine. I'm not sure but I think because the last open activity was Activity B and Android goes back to this activity? I tested it with the example above, so no, I start the main activity. When I close the app (open "recent" and remove it), then it works with your solution, but when I open the app from "recent" the I need finishAffinity. – user3215952 Aug 21 '17 at 13:08
  • Still doesn't make any sense. When you open the app from recents, if there are no active activities, it should open the app with your launcher activity, not with the last activity visited. Can you post your manifest? – David Wasser Aug 21 '17 at 14:02
  • Sure, I had to cut it. ` ` – user3215952 Aug 21 '17 at 23:54
  • You can edit your original post and add the entire manifest, and format it so it is easier to read. You shouldn't post code snippets in comments. – David Wasser Aug 22 '17 at 07:14