0

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

4

2 回答 2

3

创建一个在 onCreate = true 中设置的模块级布尔值。当 onResume 执行时,将其设置为 false - 在您通过仅应在值为 false 时运行的方法之后。这样,在onPause之后执行onResume时,onResume中的methond就会运行。

boolean skipMethod = false;

onCreate(){

    skipMethod = true;

}

onResume(){

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

}
于 2012-12-04T11:42:18.783 回答
1
public class SomeActivity extends Activity{

    bool onCreateCalled = false;

    @Override onCreate()...

       ...
       onCreatedCalled = true;
       ...

    @Override onResume()...

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

然而,这很丑陋。我建议您再次查看这个常用方法,并且只从 onResume() 调用它,因为 onResume() 总是在 onCreate() 之后。

于 2012-12-04T11:43:14.427 回答