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

前端 未结 4 1230
一整个雨季
一整个雨季 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 19:54

    Yes, whenever you activity is going to be disappeared, onPause is called.

    0 讨论(0)
  • 2021-01-17 20:01

    The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). so onPause() is guaranteed..The official documentation here

    EDIT 1

    onCreate() is to onDestroy() && onStart() is to onStop() && onResume() is to onPause() .. onStart() is called when onCreate() finishes its work. if not its not called.. onResume() indicates the ui is about to be shown to the user -(An Activity's content is the screen the user sees). if you call finish() in onCreate(), onPause() will be skipped because onResume() was never called same goes to onStart() .. so in some cases you can say its not; but that will be false, because what's an Activity that is not a screen or serve as a container for screens-(Fragment).

    and why would you call finish(); directly in your Activity's onCreate()? From how Activities work in general, onPause() will always guarantee is calling..

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-17 20:10

    It is a bit late but may help future visitors.

    I faced the problem of onPause() not being called when I tried to make an splash screen for my app following this tutorial.

    After some fiddling around and forcing my mind I figured out why! the onCreate() method looks like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    

    So before the activity gets a chance to get started or resumed I was forcing it to be finished and so bypassing the other state method calls!

    But as far as I know from the official Activity documentation in any other situation (at least normal ones!) the onPause() method is guaranteed to be called!

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