How can I know that OnResume comes after onCreate?

Lukap picture Lukap · Sep 20, 2011 · Viewed 13.8k times · Source

I have few activities and from one activity I open another and that go back to the first one...

The point is onCreate is called ones , and onResume every time when the activity is show. For example when I close B that was previouslly started from A, the onResume is called but not onCreate....

my problem is that I do not want to run the onResume if it comes after onCreate, I want to run the code only if onCreate wasn't called

Is this possible to do WITHOUT static flag ?

is there some method or flag from android like comesAfterOnCreate ?

@Override
protected void onResume() {
   if(comesAfterOnCreate){
       //DO not run this code
   }else{
      //run the long task
   }

I show a lot of answers with solution using static flag, Thanks to all of you for the effort and offering the help, but I was interested is there some method or something...

Answer

Lalit Poptani picture Lalit Poptani · Sep 20, 2011

Try this,

boolean flag = false;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

           flag = true;
....
}
@Override
    protected void onResume() {
        super.onResume();
              if(flag == true){
                 ..... // it has came from onCreate()
               }
               else{
                  .....// it has directly came to onResume()
               }
    }

Now, when the Acitivity will finish the value of flag will be false again and onResume() will be called with value false.

Hope this works for you.