0

I have an activity which displays a table from the database. When I press power button to turn off the screen, then pressing power button again to resume the activity, the table loads again and does what it did on create. Why is my activity running again when I resume it?

I even put onResume() and left it blank/default.

@Override
protected void onResume() {
    super.onResume();

}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • Can you show your `onPause`? – Mustansir Sep 11 '17 at 08:19
  • what about your `onStart`? – ColdFire Sep 11 '17 at 08:19
  • Thats simple. Because `onCreate()` is invoked again, when you turn on the screen. To check whats going on, you should proabably include a `Log.d("", "inside ")` in all of those methods and see the logs – zeekhuge Sep 11 '17 at 08:24
  • I'm sure it's not calling oncreate again. After you resume Activity It shows fragment's without calling oncreate as it was before locking phone. – Vygintas B Sep 11 '17 at 08:26
  • @Michal_196 same as onResume() – Elbert John Felipe Sep 11 '17 at 08:26
  • @A.A i did not put onStart() since I don't think it's necessary. do i need to? – Elbert John Felipe Sep 11 '17 at 08:27
  • Since you assume that `onCreate` is called again maybe the cause is that your activity was destroyed and forced to recreate (and you didn't saved its state correctly). For an activity to be destroyed, there are 2 main reasons: either the system has run out of memory or you have `Don't keep activities` activated from `Developer options`. Please check the last one to see if it is enabled. – Iulian Popescu Sep 11 '17 at 08:28
  • @Iulian Popescu In my opinion everything is working as supposed to be, but he just doesn't understand fragment lifecycle – Vygintas B Sep 11 '17 at 08:29
  • @VygintasB I agree that the underlaying problem is based on the incorrect handling of lifecycle, but there is something in user's code that makes that table to reload again, which is triggered by something (which we don't know exactly at the moment). – Iulian Popescu Sep 11 '17 at 08:37
  • @IulianPopescu I'm sure I have the necessary knowledge about the life cycle... Anyway, I put a Log in my main activity inside onCreate() that displays a text. At first run, it displayed the text, then I pressed the power button on and off and it didn't run the text again meaning it did not recreate the Activity. That's what I want. However, in the Activity where I am having issues, it keeps on recreating the onCreate() thus keeps printing the log. – Elbert John Felipe Sep 11 '17 at 08:40
  • You said: "in the Activity where I am having issues, it keeps on recreating the onCreate() thus keeps printing the log". How does this exactly happens? – Iulian Popescu Sep 11 '17 at 08:45
  • everytime I press power button on/off. I can see in the Android monitor it prints the log again and again but in my other activities it only prints once which is what I expected. – Elbert John Felipe Sep 11 '17 at 08:47
  • 3
    Ok, can you please post the full code of the activity that causes problems? – Iulian Popescu Sep 11 '17 at 08:47
  • im starting to think maybe because off memory problems. this activity is quite heavy because it loops every textview in the table row according to the number of rows in the database so maybe that's why the system destroys it when onPause – Elbert John Felipe Sep 11 '17 at 08:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154115/discussion-between-iulian-popescu-and-elbert-john-felipe). – Iulian Popescu Sep 11 '17 at 08:50
  • guys I also found out that putting android:screenOrientation="sensorLandscape" in the activity manifest runs the onCreate method twice. it's so annoying. then I found a link that says to put android:configChanges="orientation|keyboardHidden|screenSize‌​" in my activities manifest after android:screenOrientation="sensorLandscape", and it worked. lol – Elbert John Felipe Sep 12 '17 at 00:33

1 Answers1

2

Accordingly, it will call methods according to as follows, that will never call for onCreate() so that your data is reloaded,

On launching Activity it will call --> onCreate(), onStart(), onResume(),

onPower Key Off --> onPause(), onStop()

onPower Key On --> onRestart(), onStart(), onResume()

To prevent reloading your data there are many cases this and this link

May be due to :

android:configChanges="orientation|keyboardHidden|screenSize"
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142