Bring Application to Front After User Clicks on Home Button

Bring application to front after user clicks on home button

Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

You should use your starting or root activity for MyRootActivity.

This will bring an existing task to the foreground without actually creating a new Activity. If your application is not running, it will create an instance of MyRootActivity and start it.

EDIT

I added Intent.FLAG_ACTIVITY_SINGLE_TOP to Intent to make it really work!

Second EDIT

There is another way to do this. You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:

Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);

How to bring an Activity back to the front after i click home button(via notification)

Use this as this will also do the same thing in more elegant manner ( without any side effects )

  final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

or

you can use this too

<activity 
android:name=".YourActivity"
android:launchMode="singleTask"/>

Description of singleTask

The system creates a new task and instantiates the activity at the root of the
new task. However, if an instance of the activity already exists in a separate
task, the system routes the intent to the existing instance through a call to
its onNewIntent() method, rather than creating a new instance. Only one
instance of the activity can exist at a time.

Notification bring app to front without change activity

You don't ever set Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT. That flag is set by Android when it brings the activity to the front. You setting it has no effect.

There's a few ways to do what you want. Check out this answer

How to bring activity to front after home button has been pressed

After some research I have found that the way to do this is to call;

Intent i=new Intent(this, BajoConsumo.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

from a service, that will open and focus the new activity, and it will become visible.

Android moving back to first activity on button click

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(D.this, A.class));
}
});

Declare A in your manifest with the android:launchMode="singleTask". This way, when you call startActivity() from your other activies, and A is already running, it will just bring it to the front. Otherwise it'll launch a new instance.



Related Topics



Leave a reply



Submit