Mugen
Mugen

Reputation: 9095

Other activity is also brought to front on StartActivity

I have two activities (A and B) in my app and some BroadcastReceiver.

I encounter the following scenario:

A is running and was closed using the home button (onStop was called).

Some time after, BroadcastReceiver was triggered with some intent. It run the following code:

            Intent activityIntent = new Intent(context,
                    B.class);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            context.startActivity(activityIntent);

And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?

Upvotes: 1

Views: 151

Answers (2)

Dhruba Bose
Dhruba Bose

Reputation: 446

From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.

Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
context.startActivity(intent);

Upvotes: 1

Sam
Sam

Reputation: 4284

when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code

@Override
public void onStop() {
    if(!isFinishing())
       finish(); 
    super.onStop();
}

Upvotes: 1

Related Questions