0

I have an android application with several activities .Each and every activity has Application Icon in action bar which helps user to return back to main activity directly instead of pressing back button.My problem is that when I use the icon to start my home activity it does not uses the previous instance from the stack and start creating it again.

My Action bar app icon code is :

startActivity(new Intent(this, DashBoard.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

this above code starts Dashboard activity and calls its both onCreate() and onResume().But If I uses back button to return to this activity from any activity it just calls onResume().

Activity definition from manifest file:

    <activity
        android:name=".DashBoard"
        android:configChanges="keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
    </activity>

Why is this happening?Am I missing something to prevent it from not creating it again?Please help

Thanks

Anshul
  • 7,914
  • 12
  • 42
  • 65
  • Please post your activity definition from the manifest also – Thrakbad Jan 29 '13 at 08:28
  • Have you called finished on your DashBoard.class at anytime? Try to Log when your activity is destroyed to check when it happens. – Thommy Jan 29 '13 at 08:29
  • read the following post and android docs and find the suitable solution for you. http://developer.android.com/reference/android/content/Intent.html http://stackoverflow.com/questions/7385443/flag-activity-clear-top-in-android – Raj Jan 29 '13 at 08:29
  • @Thommy I never called finish() in my DashBoard activity – Anshul Jan 29 '13 at 08:30
  • @Thrakbad I have added my activity definition from manifest file – Anshul Jan 29 '13 at 08:38
  • Usually, `Intent.FLAG_ACTIVITY_CLEAR_TOP` should not cause the target activity to be recreated. It just removes everything above the target activity from the stack. I have the exact same situation in my app and it works as expected. – Thrakbad Jan 29 '13 at 08:49

2 Answers2

2

Use setFlags(), instead of addFlags(). You are on right track. Use the following code.

Intent intent = new Intent(this, DashBoard.class);    
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • `Intent intent = new Intent(this, DashBoard.class);` and then `intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);`, `startActivity(intent)` – M-Wajeeh Jan 29 '13 at 08:33
  • Why are you setting same flag twice (Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP) – Anshul Jan 29 '13 at 08:46
  • Sorry I meant to use `FLAG_ACTIVITY_SINGLE_TOP`. :P, check again please. – M-Wajeeh Jan 29 '13 at 08:48
  • wOw this worked. Could u tell me why do we need these two FLAGS together? – Anshul Jan 29 '13 at 08:51
  • 3
    Well, let me quote from http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP "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. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered – M-Wajeeh Jan 29 '13 at 08:56
  • to the current instance's onNewIntent()." – M-Wajeeh Jan 29 '13 at 08:58
0

Remove the FLAG_ACTIVITY_CLEAR_TOP.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42