why “onPause” is not called in following situation?

后端 未结 5 1015

By the document, \"onPause\" is called, when:

when the system is about to start resuming a previous activity.

Compared to \"onSt

相关标签:
5条回答
  • 2021-01-13 03:20

    This is what the official documents says about onPause()

    Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().

    When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

    For detail please see this.

    0 讨论(0)
  • 2021-01-13 03:31
    public class MainActivity extends Activity
    {
        String tag="my result";
    
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Log.v(tag,"I am in oncreate");
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.v(tag,"I am in onDestroy");
        }
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            Log.v(tag,"I am in onpause");
        }
    
        @Override
        protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            Log.v(tag,"I am in onRestart");
        }
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            Log.v(tag,"I am in onresume");
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            Log.v(tag,"I am in onstart");
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            Log.v(tag,"I am in onstop");
        }
    }
    

    Run it and check logcat.press back button and then check.after again start application and

    Press call button then check logcat now press Back button and again check logcat. you can easily understand life cycle of Activity.

    0 讨论(0)
  • 2021-01-13 03:34

    @Henry please Try yourself by testing the flow of app. Create an Activity in which Override all the methods and print log under all the methods. The flow is onCreate>>onStart/restart>>onResume and when you press home button or starts any new activity then it called onPause>>onStop and if you finish your app then your app is destroy/finish so where is confusion.

    0 讨论(0)
  • 2021-01-13 03:37

    This actually happens because when the Home key is long pressed, there is no activity being launched. The onPause/onStop will only be called if you select one of the apps present in the "Recent Apps" list.

    The docs of onPause() are pretty clear:

    Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.

    0 讨论(0)
  • 2021-01-13 03:38

    In a lot of modern phones, the recent apps list is an activity, and does trigger onPause. I've tested this on a couple of Samsung and LG phones, and the packages are com.android.systemui and com.lge.launcher2 respectively.

    That said, it seems like you can't rely on recieving an onPause when you open the recent applications screen. I'm curious to see how many device don't give you the onPause...

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