What is the difference between onPause() and onStop() of Android Activites?

后端 未结 8 1141
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 07:33

From android doc here http://developer.android.com/reference/android/app/Activity.html, it said \'Activity comes into foreground\' will call onPause(), and \'Ac

相关标签:
8条回答
  • 2020-12-02 07:45

    In concise words:

    onStop() of previous activity life-cycle method is invoked when another activity is shown. When you have Dialogue on the top of activity, there onPause() is invoked.

    Note: Activities are those components which fill your entire screen.

    Note: Dialogues are not Activity as they don't completely fill the screen.

    0 讨论(0)
  • 2020-12-02 07:45

    whenever a new ACTIVITY starts the previous activity's onPause will be defiantly called in any circumstances.

    actually there will be two circumstances:

    1- a part of previous activity is visible or the new activity is transparent: only onPause will be called.

    2- previous activity is completely covered by new activity: both onPause and onStop will be called

    ----Good to state some notes:

    NOTE 1: if a dialog starts on top of an activity NONE of onPause or onStop will be called.

    NOTE 2: if its an Activity whose theme is set to a dialog, the behavior will be just like a normal activity.

    NOTE 3: apparently a system dialog like permission dialog since marshmallow will cause onPause.

    0 讨论(0)
  • 2020-12-02 07:46

    No, if some activity comes into foreground, that doesn't necessarily mean that the other activity is completely invisible. Consider the following case:

    Activity with the theme Theme.Dialog

    Here we see both activities at the same time. The first activity with the fields is obscured by another activity, and the user can no longer interact with it. However, it is still visible with all the resulting consequences.

    That leaves a question which activity is considered fully opaque and covering the whole screen and which isn't. This decision is based on the window containing the activity. If the window has a flag windowIsFloating or windowIsTranslucent, then it is considered that the activity doesn't make the underlying stuff invisible, otherwise it does and will cause onStop() to be called. The relevant code can be found in com.android.server.am.ActivityRecord:

    fullscreen = ent != null && !ent.array.getBoolean(
            com.android.internal.R.styleable.Window_windowIsFloating, false)
            && !ent.array.getBoolean(
            com.android.internal.R.styleable.Window_windowIsTranslucent, false);
    
    0 讨论(0)
  • 2020-12-02 07:48

    Being in the foreground means that the activity has input focus. For instance, an activity can be visible but partially obscured by a dialog that has focus. In that case, onPause() will be called, but not onStop(). When the dialog goes away, the activity's onResume() method will be called (but not onStart()).

    0 讨论(0)
  • 2020-12-02 07:52

    Practically, one should consider the difference between “onPause()” and “onPause() + onStop()”.

    Whenever some new activity occurs and occupies some partial space of the Screen. So your previously running activity is still visible to some extent. In this Case, the previously running activity is not pushed to Back Stack. So, here only onPause() method is called.

    On other hands, if some new Activity occurs and occupies the full screen so that your previously running activity is disappeared. In this Case, your previously running activity is moved to Back Stack. Here, onPause() + onStop() are called.

    To Summaries-

    onPause()- Screen is partially covered by other new activity. The Activity is not moved to Back Stack.

    onPause() + onStop()- Screen is fully covered by other new activity. The Activity is moved to Back Stack.

    Know more about- Back Stack.

    0 讨论(0)
  • 2020-12-02 07:53

    Yeap, I try to understand and I can explain this below:

    There are 2 activities: ActivityA & ActivityB

    public class ActivityA extends Activity implements OnClickListener {
    
    // button
    private Button mBtnChangeActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        initialize();
        setEvent();
    }
    
    private void initialize() {
        Log.i("Activity A", "Initialize()");
        mBtnChangeActivity = (Button) findViewById(R.id.btn_change_activity);
    }
    
    private void setEvent() {
        Log.i("Activity A", "setEvent()");
        mBtnChangeActivity.setOnClickListener(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("Activity A", "onStart");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("Activity A", "onResume");
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        Log.i("Activity A", "onPause");
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        Log.i("Activity A", "onStop");
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("Activity A", "onDestroy");
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_change_activity:
            Intent activityB = new Intent(this, ActivityB.class);
            startActivity(activityB);
            break;
        default:
            break;
        }
    }
    

    Here is activity B. Follow my comment in code

    public class ActivityB extends Activity implements OnClickListener {
    
    // button
    private Button mBtnChangeActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        initialize();
        setEvent();
        // if call finish() here, activityA will don't stop, just pause
        // Activity A will call onStop() when Activity B call onStart() method
        finish();
    }
    
    private void initialize() {
        Log.i("Activity B", "Initialize()");
        mBtnChangeActivity = (Button) findViewById(R.id.btn_change_activity);
    }
    
    private void setEvent() {
        Log.i("Activity B", "setEvent()");
        mBtnChangeActivity.setOnClickListener(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("Activity B", "onStart");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("Activity B", "onResume");
    }
    
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_change_activity:
            finish();
            break;
        default:
            break;
        }
    }
    }
    

    I hope this is clearly

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