How to Bring an Activity to Foreground (Top of Stack)

How to bring an activity to foreground (top of stack)?

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)

Start Activity and Bring it to Front

First of all you need to keep context in memory to make intent's.

Secondary you need a mechanism that can re-obtaining your context every 24 hours because usually context stay alive in 24 hours.

After.

Launching Activity from service :

Intent dialogIntent = new Intent(getBaseContext(), myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

source : https://stackoverflow.com/a/3607934/2956344

To push activity on top add

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

How to put in foreground an Android activity that is in background

What you need is the FLAG_ACTIVITY_REORDER_TO_FRONT when starting a new activity. This will cause a background activity to be be brought to the foreground if it's running, or create a new instance if it's not running at all.

From inside ActivityB:

Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

How to bring an Activity to foreground (or create if not existing)?

For an activity to launch only one instance of itself, have a look at the <activity> manifest element, and particularly android:launchMode. You want to configure it with either singleTask or singleInstance.

To pass data to your activity, you add data to the Intent you use to open it. To pass data with the intent, use the putExtra() methods of the intent before sending it off, and getExtra() methods to retrieve them in your receiving activity.

I'm assuming that you know roughly how intents work, but if not you could learn more about intents by taking a look at this Android developers article.

Activity is creating again when i want to bring existing one to front

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().



Related Topics



Leave a reply



Submit