Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resume the Activity instead of Starting if already exists in back stack

I have an Activity_1 after a lot of steps, say

Activity_2 > Activity_3 .... in some Activity_n I change some data related to Activity_1 and call it using

Intent intent = new Intent(Activity_n.this, Activity_1.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

To refresh the content. But later I can go all the way back to Activity_1 where I started, which has old data.

Instead I want the initial Activity_1' s onResume() to be called, using the above code. Or appropriate Flag

FLAG_ACTIVITY_CLEAR_TOP

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.

That' what the docs say, but not what I am getting.

like image 845
Archie.bpgc Avatar asked Mar 12 '13 10:03

Archie.bpgc


People also ask

How do I start activity and clear back stack?

Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest. Now add the following flags while launching A from anywhere. It will clear the stack.

What happens to the back stack when you switch between activities?

If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.

What happens when an application calls startActivity for an activity that already has an instance running?

If you call startActivity() for an Activity with default launch mode(i.e, you didn't mention any launch mode in either in manifest or in Intent) a new instance of the activity is created.

How do you refresh an activity on a resume?

I can use this intent to refresh the activity currently: Intent refresh = new Intent(this, Favorites. class); startActivity(refresh); this. finish();


1 Answers

You can add this two lines and try

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Write this in your manifest file inside Activity

<activity      android:name=".SettingsActivity"      android:launchMode="singleInstance"      android:screenOrientation="portrait" > </activity> 

"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

You can use SingleTask or SingleInstance

"singleTask" - The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

"singleInstance" - Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

Refer this link http://developer.android.com/guide/topics/manifest/activity-element.html

like image 58
Nirali Avatar answered Sep 22 '22 10:09

Nirali