11

In my current application the onResume function is triggered when I load an Activity for the first time . I looked into the Activity Lifecycle, but I didn't find a way to prevent this from happening.

Can I prevent the onResume() function from loading when an Activity is loaded for the first time, without using SharedPreferences?

Jack
  • 430
  • 2
  • 5
  • 19
  • 2
    Why don't you want to do it? What is so important to break a consistent lifecycle of Android? Cannot you do it in the onCreate? – RvdK Mar 29 '13 at 10:25
  • possible duplicate of [Android onCreate onResume](http://stackoverflow.com/questions/6175679/android-oncreate-onresume) – Matt Handy Mar 29 '13 at 10:30
  • Possible duplicate of [Android onCreate onResume](http://stackoverflow.com/questions/6175679/android-oncreate-onresume) – M. Haché Apr 20 '17 at 14:48

1 Answers1

37

Firstly, as RvdK says, you should not modify the Android Activity lifecycle, you probably have to re-design your activity behavior in order to be compliant with it.

Anyway, this is the best way that I see:

1.Create a boolean variable inside your Activity

public class MyActivity extends Activity{
  boolean shouldExecuteOnResume;
  // The rest of the code from here..
}

2.Set it as false inside your onCreate:

public void onCreate(){
  shouldExecuteOnResume = false
}

3.Then in your onResume:

public void onResume(){
  if(shouldExecuteOnResume){
    // Your onResume Code Here
  } else{
     shouldExecuteOnResume = true;
  }

}

In this way your onResume will not be executed the first time (shouldExecuteOnResume is false) but it will be instead executed all the other times that the activity is loaded (because shouldExecuteOnResume will be true). If the activity is then killed (by the user or the system) the next time it will be loaded the onCreate method will be called again so onResume will not be executed, etc..

The Good Giant
  • 1,740
  • 2
  • 19
  • 35
  • Thank you for your answer. I also used the same method that you mentioned, but when I launch the "MyActivity" activity from another activity, the variable shouldExecuteOnResume is reset. It seems that every time I call the MyActivity activity, a new instance is created instead of it using the existing instance. I want the app to use the existing instance of MyActivity. – Jack Mar 29 '13 at 10:44
  • you can try to make the variable static but I guess the problem is that you never know if/when your app is killed by the system. You can try to put some other controls in the onStop to check what happens.. – The Good Giant Mar 29 '13 at 10:49
  • Thank you, it worked when I made shouldExecuteOnResume static. – Jack Mar 29 '13 at 10:58
  • 4
    To avoid using static make your activity single task using `android:launchMode="singleTask"`. This will make it created only once and reused every time you call this activity and keep the shouldExecuteOnResume non static – AndyW Sep 25 '13 at 22:58
  • Hello, when I tried this, I got 'super not called exception' – TootsieRockNRoll Apr 04 '14 at 17:34