2

I've got a strange problem with the Activity I'm working on,

It starts fine, then when the phone sleeps it runs onPause which is fine. Then however, when the screen is still black it runs the onCreate method again. Then when the phone comes out of sleep it runs the oncreate method for a third time... How can I stop this happening?

I mean, every time the phone goes into sleep mode and then comes out of sleep mode it runs the onCreate method twice... Really it shouldn't be running the method at all although I am using a lot of memory.

gunboatmedia
  • 435
  • 1
  • 6
  • 15

1 Answers1

3

Yes when ever phone goes to sleep it calls onPause(), but have you written anything inside the onPause()? If yes whats the code you have written in onPause()?

Arun
  • 1,658
  • 1
  • 14
  • 21
  • @Override protected void onPause() { super.onPause(); } Just the standard on pause method is what I have... I just used it to check when it's being called... Still no idea why oncreate is being called when the phone goes into sleep... – gunboatmedia Jan 04 '12 at 11:40
  • Just been looking it over again... It gets more weird, so when the phone sleeps, the onPause is called, then the onCreate, then onPause again... But the Activity is still going through it's onCreate method when the onPause is called... And it continues to do so. – gunboatmedia Jan 04 '12 at 11:49
  • Fine. In android when onCreate() is called only when Activity starts(if not already started). In your case since the activity is going to sleep it is calling onPause() and when it comes out of sleep it should call onResume(). But in your case its calling onCreate(). It means that somewhere your activity is calling onStop(). I suggest you to override onStop() and onDestroy() to make sure your activity is not getting finished. – Arun Jan 04 '12 at 12:16
  • 3
    are you sure that you are not changing orientation of your device, because it calls oncreate(). Try adding below code to your activity in manifest file android:screenOrientation="portrait" android:configChanges="orientation" – Arun Jan 04 '12 at 12:23
  • 1
    THANKS! That was the problem, the android:configChanges="orientation" was key. Even though I had forced my screen to landscape it still ran the activity in portrait when the screen is put into sleep mode(when you cant even see the activity)... I never realised it did this so thanks a lot! – gunboatmedia Jan 04 '12 at 12:36
  • @ArunMG you need to put that as an answer that was my prblem too with landscape mode set but I did not have android:configChanges="orientation", thanks a ton! – JPM Mar 21 '12 at 20:57