Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know that OnResume comes after onCreate?

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

like image 749
Lukap Avatar asked Sep 20 '11 11:09

Lukap


2 Answers

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.

like image 77
Lalit Poptani Avatar answered Oct 13 '22 10:10

Lalit Poptani


its not a difficult job, you can use boolean or see here Is that possible to check was onCreate called because of orientation change?

like image 36
Mohammed Azharuddin Shaikh Avatar answered Oct 13 '22 11:10

Mohammed Azharuddin Shaikh