How to control/modify the activity stack in my case - android

I have following activity stack: A->B->C->D
In activity D, a new Activity E will be launched. Is that possible to change the activity stack to A->E (so if user press back they will go to activity A)?
Additional information: I don't want D,E depends on A. So override E's onBackPressed to start A directly or start A from D with CLEAR_TOP flag and then go to E is not a choice.

ok as per your requirement it is possible using startactivityforresult and onActivityResult let me explain
1.first call A to B
2.from B call startactivityforresult C, also in B add onActivityResult to handle result
3.from C call D as step 2.
4. Now when you call E from D just finish D and setResult(RESULT_OK)
6.Now in activity C onActivityResult get called there check for result ok and finish activity and setResult(RESULT_OK) same for B
here you get stack A to E.
for more info http://developer.android.com/training/basics/intents/result.html
if any problem let me know

You can manage both your stack and your back pressed buttons by overriding the onBackPressed method and by using Tasks and Back Stack or not.
So within backpressed methods:
#Override
public void onBackPressed() {
startActivity(new Intent(this, YourChosenClass.class));
super.onBackPressed();
}
A task is a cohesive unit that can move to the "background" when users begin a new task or go to the Home screen, via the Home button. While in the background, all the activities in the task are stopped, but the back stack for the task remains intact—the task has simply lost focus while another task takes place, as shown in figure 2. A task can then return to the "foreground" so users can pick up where they left off. Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity.
Activities linked with a task will be available in the back stack.

Related

finish() not working properly

In my app, whenever calling the finish() method, wherever it was, I am not taken to the previous activity, rather I am directed to mainActivity.
finish();
My aim is, showing the user the activity just before the current activity he is seeing.
Question 1 : How can I make finish() always take me to the activity before ?
Question 2 : Does this work using another workaround other than finish() ?
Question 3 : How to check the stack of activities and decide accordingly which one to go to ?
If you have written finish in each intermediate activity, that means you are removing the activity from the stack, hence on finishing an activity you are taken to the last non-finished activity, hence write finish() in only that activity which you do not want to see until the same workflow is followed and its onCreate() is called
if you start activity c from b and b from a if you use the back button on your phone at activity c it will go to b and back button press in b it will show a. if you use finish in all the 3 activities what happens is a calls b and a is finished and b calls c and b is finished so when you use finish in c it will not have b to show. so you have to tell where you are placing your finish based on some condition or a button click or just before starting a new activity, post your code and we will help you.

How to go back to Home Activity from a specific activity without overriding back button

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();

Do I need to explicitly finish an Activity in Android?

When I am starting a new activity do I need to explicitly finish the current activity or does android take care this ?
This is what I write in activity A to start activity B:
Intent intent = new Intent(this, BActivity.class);
startActivity(intent);
Should I end A by calling next line after above mentioned two lines ?
this.finish()
In General no you shouldn't.
The difference will be if you call finish in Activity A, While the user is in Activity B if they press the back button they will go back to whatever they were doing before opening your application. If you instead do not call finish in Activity A they will go back to Activity A
If you DO call finish:
Activity A -> Activity B -> [user press back] -> Homescreen (or whatever activity is on the stack below activity A)
if you DO NOT call finish:
Activity A -> Activity B -> [user press back] -> Activity A
No it is not compulsory.
finish()
finish method state that "Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()."
reference link >> link
explicitly finish the current activity or does android take care this ?
It depends on your requirement if you wants activity A while coming back form activity B still there so you need not to call finish but if you does not want activity A when coming back form activity B then you should call finish ....

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.

Clearing stack including activities in different tasks

I have a main activity A. There are two scenarios
1) A launches B.
B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK.
now I have a menu option in B which performs a delete operation and starts the activity A.
2) A launches B, which launces C it also contains the menu option to perform delet opereation.
I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there
I am stuck is there a way to clear the stack.
try using following code on delete opereation on both B and C activity
Intent intent=new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..
And your stack now will be only A instead of A-B-C-A.
and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.
Hope you got some trick.
Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.
That will act like
A-B-c to A
Why not finishing B and C before calling A again?
finish();
My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.
Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.
Hope this will help you :)

Categories

Resources