How to save instance of an activity? - android

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.

Related

How to prevent to relaunch specific Activity after pressed back button?

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?

Preload Activity in backstack

When user click on a notification, I set up a backstack of Activities A -> B, where B is on top and shown to user. I would like the lifecycles of Activity A to run, so that when user presses back button and comes to Activity A, it is already ready. What could I do to achieve this?
This cannot be possible as android will not allow us to do that, the only way I can see you can achieve this by doing your Activity A operation inside your Activity B And provide those details to Activity A when the user pressed back button.
Note: If you are showing any kind of list on Activity A or doing similar kind of work, you can have one Singleton Class where you can get your data in Activity B which required by Activity A, and provide same data to Activity A when the user pressed back button.
You should open the activity as normal but then put this line of code in the OnCreate() method to bring to the back.
moveTaskToBack(true);

Differentiate home button pressed, back button pressed and new activity launched

I have a FragmentActivity. Within its onPause function, I would like to differentiate
Home button pressed
Back button pressed
Launching a new activity (This will cause the fragment activity's onPause being called)
For back button pressed case, I know I can differentiate it by using this.isFinishing() == true.
However, how about launching a new activity case?
I know perhaps I can set a flag before launching the new activity, and reset the flag, at the end of onPause function. But, it doesn't sound elegant to me. Is there any better and robust way?
You can create a singleton class that tracks when your activities start/resume/pause/stop. Make a base Activity class that calls the singleton in each of its lifecycle callbacks.
If you look at the order of the callbacks when transitioning between activities, you should see this:
Pause Activity A
Create Activity B
Start Activity B
Resume Activity B
Stop Activity A
When your singleton gets called from Activity A's onStop(), you can check if there was a call from onResume() from another Activity (which clearly belongs to your app). If there wasn't, you know the user has changed apps or gone back to the home screen.
As for the back button press, you can check isFinishing(), or override Activity.onBackPressed() and do your bookkeeping there.

Killing Activities using Back Button

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.

Resuming back to an activity without restarting the activity

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.

Categories

Resources