onCreate flow continues after finish()

后端 未结 2 443
小鲜肉
小鲜肉 2020-12-03 09:46

I would like to finish an activity from inside the onCreate method. When I call finish(), onDestroy() is not immediately called, the c

相关标签:
2条回答
  • I'm guessing that it is because finish() doesn't cause the onCreate method to return. You could try simply adding

    finish();
    return;
    

    Or use an if else

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layoutb);
      if(good data){
          //do stuff
      }else{
          finish();
      }
    }
    
    0 讨论(0)
  • 2020-12-03 10:14

    It seems like finish() does not work until onCreate() return control to system. Please refer to this post: about finish() in android. You have to consider this issue if you don't want any of your code to be executed after calling finish.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题