Android task and back-stack issue - android

I have 3 activities: A, B and C.
A -> B -> C -> B (means A launches B, B launches C and so on)
B is singleTask, C is standard
Is possible to let C shown when user press BACK button in 4th activity (that is, the last activity, B)?

Change Task B from singleTask back to standard. You are not using singleTask correctly. singleTask launch mode is meant to be used for activities that are at the root of a task.
You can only have one instance of a singleTask activity on your device. When you "launch" B from C, you are just returning to the already existing instance of B. So there is no fourth activity.
More Info at Android Activity - Launch Mode

I think use can use
moveTasktoBack(); in Activity class

Related

Launching activity from a singleinstance activity

I have 3 activities:
A, B , C
A has singleInstance launchMode.
A is the main activity, which launches B, B launches C, then I press the home button and open the app again.
A is opened, but when A tries to launch B, the existing stack is opened instead (returns to existing C)
Is this the expected behaviour?
If A is singleInstance, when A launches B, Android automatically adds FLAG_ACTIVITY_NEW_INSTANCE to the Intent (because A is singleInstance) and because of that it creates a new task with B at the root of the task. Then B launches C. Andriod launches C into the same task as B (this is the normal behaviour). So now you have 2 tasks: One with just A in it, and another which has B at the root, with C on top of that.
Now you go back to the task containing A. When A launches B again, Android again adds FLAG_ACTIVITY_NEW_TASK to the Intent). Then, because FLAG_ACTIVITY_NEW_TASK is set, Android looks for an existing task that has B at the root. Since it finds one, it simple brings that task to the foreground in whatever state the task was in when that task went to the background. This is the normal and expected behaviour.
NOTE: This is just one more reason NOT to use the special launch modes singleInstance and singleTask, because they have lots of nasty, undocumented, misunderstood side-effects.

Reorder existing activity to the top when start it from other applications

I have application A, and application B.
In A, it has a, b, c activities.
In B, it has x activity and there is a button to launch b activity in A.
Assume in back stack there are already activities a, b, c (c at the top) in task 1.
Now get c to start an activity x, result in x is created in task2. (Based on what I read from the introduction on lollipop).
Then click the button in x, it will start b activity. b is then created in a separate task. I consider it normal because the launch mode is standard. If I launch b with Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT flag (For some reasons, I cannot give b singleTask launch mode, so use the flag instead). b is brought to front, however, c is killed (Based on what I read, this is expected behavior).
Now I want to know how can I just reorder b to the top without destroying c? which will make the back stack become a, c, b.
I have tried FLAG_ACTIVITY_REORDER_TO_FRONT but does not work.
I ended up adding a blank activity in application A, which is called by x, this brings task 1 to the top, then in this activity, I launch b with FLAG_ACTIVITY_REORDER_TO_FRONT flag and finish itself or just noHisotry flag in manifest file.

Go back to Activity without recreating (without invoking onCreate())

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?
There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.
When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

Getting back to the first activity via intent, what happens to the activity stack?

I have an application which pulls data from a webserver, putting this data in a listview and then presenting it to the user. There are 4 activities involved, which can be called like this:
A -> B -> C -> D
or
A -> B -> D
Basically all the activities except A are pulling data from the web. Should there be any problems with the connection and there is a timeout coming up I want the activities B, C and D to inform the user and get back to A.
So what I did right now is, I set A to launch mode singleTask. This way I can catch the timeout exception and call a new intent starting A. But what happens to the activities in between? Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A, but what about B and C? Does android automatically call onDestroy on these? What happens to the activity stack? Any hints appreciated.
Cheers
When you launch activity A, from activity D, set the intent flag:
FLAG_ACTIVITY_CLEAR_TOP
Using this flag will clear any activities in between A and the activity you are in, bringing A to the front. You also probably don't need to be using singleTask as a launch mode.
Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A...
With your example, if all the child activities are launched with startActivityForResult() and activities B & C implement:
finishFromChild(Activity activity) {
...
finish();
}
When activity D calls finish() after it has timed out, then each child will close in order (D -> C -> B -> A) with a chance to return any relevant data you might want to salvage.

How to clear current activities in the stack?

Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.

Categories

Resources