Vic Vuci
Vic Vuci

Reputation: 7051

FLAG_ACTIVITY_CLEAR_TOP calls onCreate() instead of onResume()

So I have an abstract class extended through the entire app that overrides the back key to reorder Activity A to the front (With the flag).

So, it would be:

A > B > anywhere, and the back key should bring me back to A

I'm using the FLAG_ACTIVITY_CLEAR_TOP, but it is entirely refreshing A for some reason and I don't want that.

So: Flag_activity_clear_top is reloading the onCreate() rather than onResume(). What gives?

Upvotes: 9

Views: 9754

Answers (3)

aanshu
aanshu

Reputation: 1602

Intent intent = new Intent(CurrentActivity.this, ActivityNeedOnTop.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                    CurrentActivity.this.finish();

Upvotes: 1

Bobbake4
Bobbake4

Reputation: 24857

If you want the activity to just be brought to the top without restarting it set the launchMode of the activity to singleTop in the manifest. You will receive a call to onNewIntent when the activity is being brought to the top. onNewIntent is called before onResume. If you only want this behavior for the specific intent you can add the FLAG_ACTIVITY_SINGLE_TOP(in addition to FLAG_ACTIVITY_CLEAR_TOP) to the intent with the addFlags call instead of the manifest.

Upvotes: 25

Vishal Vyas
Vishal Vyas

Reputation: 2581

From the API docs for FLAG_ACTIVITY_CLEAR_TOP

For example, consider a task consisting of the activities:
A, B, C, D. If D calls startActivity() with an Intent that
resolves to the component of activity B, then C and D 
will be finished and B receive the given Intent,
resulting in the stack now being: A, B.

**The currently running instance of activity B in the above example
will either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new intent.**

So I think your activity is itself finished and restarted.

Upvotes: 0

Related Questions