Reputation: 2521
I am developing an app in which i have create three activities
Here is the process in which these Activties called:
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??
Upvotes: 1
Views: 1951
Reputation: 2521
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.
Upvotes: 2
Reputation: 10129
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.
Upvotes: 1