Question

I have an activity that starts a async task that listens for messages from a server. When the user hits the home button the activity is paused and the async task continues to run in the background. When a certain message arrives I would like the activity to resume and come to the front without being recreated. I have tried the following.

android:launchMode="singleTop"

intent = new Intent(getBaseContext(), MainActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

This brings the activity to the front but restarts the activity.

I have also tried

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);            
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);                   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

But none of these bring the activity to the front.

What are the correct flags to just resume the app, like what happens if I relaunch it from the home screen.

Was it helpful?

Solution

I found the following solution that seems to work. It is a bit convoluted but does what I want.

See: Bring task to front on android.intent.action.USER_PRESENT

I made the following method to call the BringToFront activity that simple calls finish();

private void bringToFront()
{
   Intent i = new Intent(getBaseContext(), BringToFront.class);
   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   getBaseContext().startActivity(i);
}

It seems to me that there should be a simpler way to bring a task to the front. Will leave open for a bit to see if a better solution is suggested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top