I have 2 activities, activity A and activity B. Both the activities have fragments. Activity A calls activity B.
When activity B is called and I press back button, on medium screen size the following function sequence is called.
onResume() of activity A.
but when activity B is called and I press back button, on large screen size (10.1') the following function sequence is called.
onCreate() of activity A.
onCreateView() of the fragment associated with activity A.
onResume() of Activity A.
Both the activities are restricted to portrait mode in the manifest and I also used
android:configChanges="orientation|keyboardHidden|screenSize"
for each activity in the manifest.
What I want is, that the application behave like it behaves on the medium screen size. i.e i don't want onCreate() of activity A to be called when I press the Back Button.
Any help will br appreciated and thanks in advance.
Save your activity state in onsaveinstancestate() then check for the bundle before all the onCreate code.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Related
I just read the following from Android Developers docs:
Here's the order of operations that occur when Activity A starts
Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen,
its onStop() method executes.
I just want to know when an activity can start another and still be visible(a concrete example). (because it means that onStop() is always calls when I switch activities)
You can start an Activity B like a dialog if you specify a flag in AndroidManifest.xml:
<activity android:name=".ActivityB"
android:theme="#android:style/Theme.Holo.Light.Dialog"/>
Then Activity B will not take the full screen and you will still see the underlying activity.
onStop() won't be called if you previous activity is visible to user.
onStop will be called only after previous activity is completely invisible to user.
So if new activity is dialog or some transparent background, then previous activity will be displayed to user and onStop() of previous activity won't be called.
EDIT: Solution to this question is - Android - Query regarding activity lifecycle on changing screen orientation
I'm starting Android development and am facing problems regarding activity lifecycle. I have an Activity A that calls Activity B in its onCreate() method (Activity B has a custom grid view inside a relative layout).
When in Activity B, if the screen orientation remains unchanged and back button is pressed, as expected it goes to Activity A's onResume() method. However, on changing screen orientation while in Activity B, on later pressing back button, it goes to Activity A's onCreate() method thereby recreating Activity B (as Activity A calls B in its onCreate()). Only on pressing the back button twice does it go back to Activity A's onResume() method. How do I ensure that on changing orientation, I go to Activity A's onResume() by pressing back button just once?
I've tried including:
android:configChanges="keyboardHidden|screenSize|orientation"
in Activity B in manifest and hence this avoids calling Activity B's onCreate on changing orientation but it still doesn't solve my problem.
Activity A is base, all onResume and onBack work with it, End.
On head of that you start Activity B, with it's onResume and onBack classes.
On rotation, B is re-created, not A, since you are in B.
I think you must change the way you start B.
Or onResume of B, finish() B. then in onResume A start B again.
I found the solution to this. Since the screen orientation had changed while in Activity B, Activity A was getting recreated (different orientation than earlier). So all that was needed was to add
android:configChanges="keyboardHidden|screenSize|orientation"
to Activity A in manifest.
I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.
I have two activities A and B. in A I start B activity. In B acitivity when user press back button I need to update A's layout values. So I need to know
In acitivity A which function will be called when return back to activity A.
try and override onResume() function in A. works for me.
Take a look at the Android life cycle: http://developer.android.com/reference/android/app/Activity.html
You will see that when you return to Activity A, function onResume() will be called.
A key point when learning Android API is the Activity Lifecycle.
When an activity is displayed : it's onResume() method is called. So it will be called:
when the Activity is displayed for the first time (after onCreate(...))
every time the activity came to front
after activity re-created (for instance, after screen rotation)
For more details read this : activity pausing-resuming and more global overview of the lifecycle.
The Activity javadoc is also very good resource
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.