3

I've added REORDER_TO_FRONT flag to bring my existing activity to top instead of creating a new instance. But it calls onCreate method of the activity instead of onNewIntent in MainActivity. If i add launchMode="singleTop" in Menifest, then works fine. But i don't want to add singleTop because in some cases in minimized mode if user click on app icon, then app restarts instead of resuming from same place.

    Intent intent = MainActivity.newIntent(context);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

Thanks

Usman Rana
  • 2,067
  • 1
  • 21
  • 32
  • Hey @Usman Rana Do you want to finish the current activity and then show the previous activity or show the previous activity and hide the current activity? – Gourav Jan 04 '19 at 11:48
  • i want to clear all activities including current and bring existing instance of MainActivity to front. – Usman Rana Jan 04 '19 at 12:00
  • Check my answer, please accept if it works! – Gourav Jan 04 '19 at 12:49

3 Answers3

4

You just need to add the SINGLE_TOP flag to your Intent, like this:

Intent intent = MainActivity.newIntent(context);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This will remove all activities from the stack back to the existing instance of MainActivity. It will NOT create a new instance of MainActivity or call onCreate(). It will call onNewIntent() in MainActivity().

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
David Wasser
  • 93,459
  • 16
  • 209
  • 274
1

If you want close all other activities and start only main activity, this should work for you:

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Hope this can help :)

unedim
  • 91
  • 8
  • I want to not start a new instance of MainActivity but bring existing instance to top to avoid call to onCreate method again – Usman Rana Jan 07 '19 at 06:57
0

No need of clearing the current activity and show the previous activity. Just use finish() method as shown below:

You can use this on your onBackPressed() and toolbar back button click:

Use this code outside onCreate():

@Override
public void onBackPressed() {
    finish(); //closes current activity
}

On the toolbar:

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         finish(); //close current activity
    }
});

The onBackPressed() method is called when the Android Back button is clicked. Its default value is super.onBackPressed() which also calls the finish() method! This example was just for reference.

The toolbar.setNavigationOnClickListener(...) method is called when the toolbar back button is clicked. But for this you have to add these lines in the code in the onCreate() method:

Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

The finish() method deletes the current activity from backstack and shows the previous activiy stored in the backstack!

Gourav
  • 2,746
  • 5
  • 28
  • 45