Android back stack - android

I have an application with several Activities in Android A,B,C,D. I have a question about the back stack.
A->B->C->D it is a normal sequence.
My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that
A1->B1->C1->D1
the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??
thank you

You should call finish() in activity C after, starting of activity D
startActivity(intent);
finish()

Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.

Use onActivityResult() methods of an activity:-
Here is how it will work -
Use startActivityForResult() method while you are starting activity
While when you are going back
Activity D - before finishing write -
Intent i = getIntent();
setResult(RESULT_OK);
finish();
Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().

Related

Android Multiple Screen Navigation

I have 3 activities to navigate. like Activity A, B, C.
From A to B i called startActivityForResult() method and overwrite onActivityResult() method. In B activity onBackPressed() i set the setResult() method and it is working fine.Now from B to C activity I again called startActivityForResult() and overwrite onActivityResult() in B and in C when back pressed I set setResult() and then call finish().
Now the issue is when I back pressed from C it will directly going to the A. I want the back navigation from C-->B--->A
Kindly help me to achieve this.
Dont call finish() in Activity C.
onBackPressed() finishes your activity. Beacuse of finish() you are going back one more step.
Remove call to finish() and everything should work as you expect.

Android: How to pass back key to the activity behind

All:
Is there a way that can pass back key to the activity behind?(Two activities A,B; B is on top of A; How to let B ignore back key and let A receive back key?)
Thanks a lot!
BR.
use StartActivityForResult(Intent, int) if you start Activity B from Activity A.
Then in B: set the result on the Intent through getIntent() and finish() activity B
then override onActivityResult in Activity A to get the value
start activity B with following flags added to that intent.
intent.setflags(INTENT.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent);
then your back will be the back from your previous activity.
Probably you might exit the app if there are no activities behind A
or another simple solution is,
call finish() after you call startActivity(...) for B in A.

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

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 ....

Android finish parent Activity

I have 3 different activities in a TabGroupActivity. Let's say A - Parent, B - Child 1, C - Child 2.
A --- starts --> B
B --- starts --> C
and I have an alert dialog in C which shows some message. I want to go back to activity A when I press Ok button on dialog.
But the problem is that I can open activity C from other activities too and I want to go back to their parent activities too. So I need to make something which will work no matter which activity opens C. I've tries with this one but didn't work :
Intent intent = new Intent(Synchronization.this,Synchronization.this.getParent().getClass());
but it didn't help me. Any suggestions?
You just have to make use of two Activity methods viz. startActivityForResult() and onActivityResult()
Example : http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Here goes the logic :
In ActivityB
Start ActivityC by using startActivityForResult(activityCIntent,INT_CODE);
In ActivityC
Now check if Dialog's OK Button is pressed, if yes then set the result using setResult(RESULT_OK,intent); and then call finish();
Then control will be redirected to ActivityB's onActivityResult() method.
Now inside onActivityMethod() check whether result_code==RESULT_OK and requestCode = INT_CODE. If yes then simply call finish();
In the Activity B start the C Activity as startActivityForResult() so when you finish the C activity it will back to the B with result. As the result you may pass flag with the intent object.
Now when you finish the C activity with ok button then set the result as RESULT_OK into the setResult() if you need to pass the data back to the B activity you may set the data into the Intent add this intent with the setResult() method and then finish the C Activity.
Now in B check the requestcode is from the C then finish this Activity. As you start this C Activity you can also start the B Activity for the A Activity.
And you need to override onActivityResult() in B activity and if you start the B Activity as for result then also you need to define also into A Activity

Categories

Resources