How to execute a method just after the second onResume?

I have two differents methods that are executed on onCreate() and onResume(), but I want that when onCreate() is executed, the method in onResume don't run. Have some way to achieve this?

--EDIT--

I removed the piece of code from onCreate(), and created a flag to interact with onPause() and passed the boolean flag as params:

Boolean flagFirstTime = true;

@Override
    public void onResume()
    {
        super.onResume();
        new asyncTask().execute(flagFirstTime);
    }

    @Override
    protected void onPause() {
        super.onPause();
        flagFirstTime = false;
    }

Before I asked, I was thinking put a flag, but in my opinion, this is very ugly. XD

Upvotes: 0

Views: 1507

Answers (2)

Simon
Simon

Reputation: 14472

public class SomeActivity extends Activity{

    bool onCreateCalled = false;

    @Override onCreate()...

       ...
       onCreatedCalled = true;
       ...

    @Override onResume()...

       ...
       if !onCreatedCalled){doSomething();}
       ...

However, this is ugly. I recommend that you look again at this common method and only call it from onResume() since onResume() always come after onCreate().

Upvotes: 1

Roy Hinkley
Roy Hinkley

Reputation: 10641

Create a module level boolean that is set in onCreate = true. When onResume executes, set it to false - after you pass by the method that should only run when the value is false. In this way, when the onResume is executed after an onPause, the methond in onResume will run.

boolean skipMethod = false;

onCreate(){

    skipMethod = true;

}

onResume(){

    if(!skipMethod){
       myMethoed();
    }
    skipMethod = false;

}

Upvotes: 3

Related Questions