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.
Related
When I pressed the back button on B Activity to go back A Activity, B Activity is relaunched 'cause B Activity was launched from intent.extra at onCreate of A Activity.
The intent.extra is recreated with previous data, so it happens recursively.
My intention is to load B Activity from FCM and then back to the A Activity when the user pressed the back button.
I already tried to remove data of intent after startActivity but it didn't work.
But If I check savedInstanceState is null or not, it works but I'm not sure it's the right way or not.
Plus, the A Activity's launchMode is singleTop but it's not the point of this problem, I think.
Whole scenarios are the following.
Get the data from FCM in the background and then the data is served from the System tray.
Check if there is data or not at onCreate of A Activity and then if there is launch B Activity.
Pressed back button on B Activity but B Activity relaunched at onCreate of A Activity.
How to prevent the action of 3 in the scenarios?
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?
Can someone help me replicate the following scenario:
I'm having a activity A with some specific tasks from which the app goes to activity B. When the user goes to B and presses back the app should close (maybe meaning that activity A performed finish()). But in some cases the app (not on the user action) goes back to Activity A, with the state preserved to when it went to B. The thing is that the app can never know if it should preserve activity A to be re-displayed later or not.
Hope I made it clear. Many thanks
You can start the Activity B with startActivityForResult() and in function of the result, when we come back to the activity A, Activity A finish() himself or not.
You can populate your Activity A every time in its onResume(). For persistence you can use SharedPreferences.
You can setup a BroadcastReceiver in Activity A which will call finish() for Activity A upon receiving a message. In the Activity B just before you close you then send that message to Activity A's broadcast receiver.
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'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.