Android: Is onPause() guaranteed to be called after finish()?

前端 未结 4 1260
一整个雨季
一整个雨季 2021-01-17 19:09

Couldn\'t find a solid answer to this anywhere. I have a method where finish() is being called, and onPause() is called afterward.

Is onPause() guaranteed to be cal

4条回答
  •  长发绾君心
    2021-01-17 20:08

    Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate().

    public class MainActivity extends ActionBarActivity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        finish();
      }
    
    
    
      @Override
      protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
      }
    
      @Override
      protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
      }
    
      @Override
      protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
      }
    }
    

    Run, and observe that your log will only contain "onDestroy". Call finish() almost anywhere else and you'll see onPause() called.

提交回复
热议问题