User42590 User42590 - 1 month ago 17
Android Question

OnCreate Calls After OnPause in Android

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??

Answer

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.