I'd like to kill all activities when the user hits the back button and returns to my main activity A. My App has the following 3 activities:
A -> Displays users selections before they hit search and creates activity B.
B -> Displays a list of urls's based on the users selection and creates activity C.
C -> Opens a Webview and displays the page selected by the user.
Currently, i don't call finish() after starting activity B so when the user hits the back button in activity C they can return to the list of url's and make another selection if they want.
I'd like to create a new activity A (without the users initial selections) and ensure the existing activities B and C are killed if the user hits the back button in activity B?
Any help would be appreciated.
Thanks
O.
You can return to activity A by overriding the back button handler in activity C and launching activity A with the flag FLAG_ACTIVITY_CLEAR_TOP set, or alternatively (perhaps preferably in your case), you can just set noHistory for your activity B.
You'll have to reset activity A manually in some way in this case though.
Related
Actually the scenario is a bit more complex than described it the title.
The situation is the following:
Activity A starts Activity B.
Activity A must not be destroyed when I start Activity B because I
need the user to be able to navigate back to A.
When the user presses the HOME button the user opens the Recent Apps
window and switches from my app to another app. At this stage both A
and B are STOPPED.
When the user user opens the Recent Apps window and switches back for
the other app to my app: Activity B is RESTARTED (activity A is not
restarted yet)
Now on Activity B there is a button to close the entire app, closing
both B and A, and it does close both activities using this approach:
https://stackoverflow.com/a/11509279/1815311
THE PROBLEM IS THAT WHEN ACTIVITY B TRIES TO CLOSE BOTH B AND A, IN
THE DESCRIBED SCENARIO, ACTIVITY A SOMETIMES IS NULL !!
How do I cope with such a scenario?
1 solution is - before finishing the activity B, store some variable with value 1 using sharedpreferences, finish activity B but not A. system will resume activity A. override onResume() function in activity A and get the variable from shared preferences, if it states 1 then store 0 there, and finish() activity A.
2 solution is overriding onresultactivity - see here
How to kill an application with all its activities?
I have an App with a Navigation Drawer design and some activities (I know working with Fragments would probably be easier here but that wasn't feasible).
When the user clicks a drawer item, the according Activity is started with FLAG_ACTIVITY_REORDER_TO_FRONT. The up button should always take the user back to the start activity A, so when the up button is pressed I also start the A activity with the same flag.
Consider the following (capital letters are the activities, trying to visualize the backstack here):
A
user starts B from the drawer
A -> B
user presses up button, brings A in front
B -> A
user presses back button finishing A going back to B
B
user presses back button again which exits the application, but imho the user should always see the start Activity A again before leaving the app.
How would / did you guys solve this?
EDIT: #Neil, with that is: If I have
A -> B
and from there via drawer go to C I have
A -> B -> C
If the user now presses the Up button and I just close C instead of bringing A to the top, the user will be back in B which would be wrong because C is (navigationwise) not under B but they are 'siblings'.
Try to make the launchMode of that Activity B to SingleTask in your manifest file like this :
android:lanchMode="singleTask"
Your activity will not be called again.
I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.
I have a splash activity (A) that calls a listview activity (B) which calls another activity (C).
When I'm on activity C and I press Home, than kill the app (or wait of Android to do it), than longpress Home and come back to activity C there's a strange problem:
When I click back I go back to B. Than I have a backbutton handler that asks the user if they want to exit and calls finish() on the activity. When I try to exit in this scenario, activity A starts again.
On regular operation it finishes B and doesn't go back to A.
Why is that??
Thanks
When the app is killed (either by you or by Android) the process hosting your applications is killed. However, Android remembers the state of the activity stack (in your case A->B->C).
When the user returns to the app, Android creates a new process for the app and recreates only the activity that was at the top of the activity stack (in this case: C). Now the user presses BACK, which causes activity C to finish and Android recreates the instance of activity B which is then shown (You will see calls to B.onCreate(), B.onStart() and B.onResume()).
Now the user presses BACK again. Your back button handler tries to call finish() on activity A, but there is no instance of activity A. Android hasn't created it yet! When activity B finishes Android remembers that there was an instance of activity A in the activity stack underneath B so it recreates the instance of activity A which is then shown (You will see calls to A.onCreate(), A.onStart() and A.onResume()).
I hope this explains what you are seeing.
Make sure you are calling finish() on A when you load B
am having a two activities that interact.
activity A picks input values and sends to activity B for the user to confirm input before submitting. activity B should allow the user to go back to activity A to edit input values if required or submit the values if they are ok. If user submits values, activity A should be finished and if he edits, then he goes back to activity A.
I have used startActivityForResult() and setResult() methods to kill the activity A when the user submits the values that has worked perfectly, but when i click the edit button to return to activity A, it call the activity A using a new Intent, and subsequently, it starts the activity A, yet previously started activity A is still running . what i want is to resume the activity A where it was left with the existing values before activity B started on click of the back button. How do i achieve this?? Your assistance is highly appreciated
Call finish() in Activity B when you need to edit. There is no need to fire an intent that creates new instance of Activity A. It will resume Activity A.