Home » Android » java – Bring activity front if it is running or create new one with backpress

java – Bring activity front if it is running or create new one with backpress

Posted by: admin June 15, 2020 Leave a comment

Questions:


Let me explain detailed; I have notification and this notification opens B activity with two cases.

Cases :

  1. If app is closed. (not running on background)
  2. If app is opened. (on background or front)

Case-1

I click to the notification and it opens the B activity with case-1. When i press back i want to go to the A activity and kill B activity. I dont need B activity anymore. Everything easy from here without using flags. When I’m on B activity and press back two times from here, it goes A activity and then closes the app. My trouble here is, if i open the app from navigation buttons of phone (can’t remember the name of this button) app is opening from B activity. That’s not what i expected. I want to open A activity. Don’t want to see B activity anymore.

Case-2

I click to the notification and it opens the B activity with case-2.When i press back i want to bring A activity to the front, without creating anything new. If i press back on B activity, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity.

So how can i make this correctly, i tried to use flags (i already read docs) but couldn’t get work.
What flags should i use when i open the B activity and onBackPress method of B activity to go A activity as i wanted

How to&Answers:

This should be achievable by adding

android:launchMode="singleTask"

to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.

If it’s not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class

Answer:

try this

 Intent intent = new Intent(context, YourActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
 startActivity(intent);

Answer:

try this

android:launchMode="singleTask" in android manifest file

Answer:

You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT

    Intent i = new Intent(context, Activity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(i);

Answer:

You May try this isTaskRoot() Which will return B is root

if it is true then launch A

other wise you may finish B

B Activity

@Override
    public void onBackPressed() {
        if (isTaskRoot()) {
            //call A which is not exist
            Intent i =new Intent(B.this,A.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }else {
            //Finish B if A Already Exixt
            finish();
        }
    }

You can call B Activity on Notification Click

if A is present then u can finish B else you can launch A

If set FLAG_ACTIVITY_CLEAR_TOP, the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

enter image description here

Answer:

Just put this in ActivityB onBackPressed:

Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();

How solve case 1:

finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.

How solve case 2:

With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.

Answer:

If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B

      android:noHistory="true"

This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.

As for having Activity A start when Activity B is killed @Quentin Menini’s answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as @Naimish Vinchhi has suggested, will have the desired effect in this instance.

https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags

https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options