I am developing an app in which i have create three activities
Main_Activity
Second_Activity
CustomView_Activity
Here is the process in which these Activties called:
MainActivity calls Second_Activtiy and in OnCreate of Second_Activtiy I have created object of CustomView_Activity.
In Main_Activity I have a button on a LinearLayout which onClick calls Second_Activity in Second_activity OnCreate i have to keep my Screen On and and declare other things like buttons layouts etc.
In CustomView_Activity i have creates canvas and onCanvas i have draw a Rectangle.
Now when my Second_Activity calls after Main_Activity and calling OnPause on pressing Power button my activity calls
OnPause -> OnStop -> OnCreate -> OnResume -> OnRestart -> OnDestroy
and when I press the power button to on the screen OnResume -> onCreate -> OnRestart calls.
Due to calling of OnCreate again and again my activity not performing tasks correctly.
Please anyone who can help??
when orientation changes again Oncreate is called. That is why your activity is not running correctly after the first time.
To fix this you have declare this in your Manifest file where the activity is declared:
android:configChanges="keyboardHidden|orientation"
For android 3.0 and above
android:configChanges="orientation|screenSize|keyboardHidden"
When the app is in landscape and the phone is locked, then the app reorients to portrait and hence onCreate gets called again. To prevent this add the above line.
You should never rely on your task stack maintaining all the state you need. Instead, save your activity's state using onSaveInstanceState() and have each activity base its actions on this state or the intent passed to it. By designing your activity flow this way, the activities can be created / destroyed by the system at its will (usually based on memory needs) and your activities will be able to pick up where they left off.
Related
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 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
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 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.
If I launch an android activity with flags ClearTop, and use SetContentView to switch views will these activities still suspend in background and follow the typical workflow with onRestart, onPause, etc.?
All these activities are fired off using startActivity btw...
Any activity follows Activity Lifecycle. If it has clear top flag then the activities above, if any, all get closed (there will be onStop and onDestroy) and that clear top activity becomes the top one because of this. For that activity there will be onStart and onResume