What is Activity.finish() method doing exactly?

前端 未结 12 1831
别那么骄傲
别那么骄傲 2020-11-22 16:16

I\'m developing android applications for a while, and followed a lot of posts about activity life cycle, and application\'s life cycle.

I know Activity.finish

相关标签:
12条回答
  • 2020-11-22 16:28

    In addition to @rommex answer above, I have also noticed that finish() does queue the destruction of the Activity and that it depends on Activity priority.

    If I call finish() after onPause(), I see onStop(), and onDestroy() immediately called.

    If I call finish() after onStop(), I don't see onDestroy() until 5 minutes later.

    From my observation, it looks like finish is queued up and when I looked at the adb shell dumpsys activity activities it was set to finishing=true, but since it is no longer in the foreground, it wasn't prioritized for destruction.

    In summary, onDestroy() is never guaranteed to be called, but even in the case it is called, it could be delayed.

    0 讨论(0)
  • 2020-11-22 16:29

    When calling finish() on an activity, the method onDestroy() is executed. This method can do things like:

    1. Dismiss any dialogs the activity was managing.
    2. Close any cursors the activity was managing.
    3. Close any open search dialog

    Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed

    0 讨论(0)
  • 2020-11-22 16:29

    My study shows that finish() method actually places some destruction operations in the queue, but the Activity is not destroyed immediately. The destruction is scheduled though.

    For example, if you place finish() in onActivityResult() callback, while onResume() has yet to run, then first onResume() will be executed, and only after that onStop() and onDestroy() are called.

    NOTE: onDestroy() may not be called at all, as stated on the documentation.

    0 讨论(0)
  • 2020-11-22 16:31

    @user3282164 According to the Activity life-cycle it should go through onPause() -> onStop() -> onDestroy() upon calling finish().

    The diagram does not show any straight path from [Activity Running] to [onDestroy()] caused by the system.

    onStop() doc says "Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called."

    0 讨论(0)
  • 2020-11-22 16:34

    Various answers and notes are claiming that finish() can skip onPause() and onStop() and directly execute onDestroy(). To be fair, the Android documentation on this (http://developer.android.com/reference/android/app/Activity.html) notes "Activity is finishing or being destroyed by the system" which is pretty ambiguous but might suggest that finish() can jump to onDestroy().

    The JavaDoc on finish() is similarly disappointing (http://developer.android.com/reference/android/app/Activity.html#finish()) and does not actually note what method(s) are called in response to finish().

    So I wrote this mini-app below which logs each state upon entry. It includes a button which calls finish() -- so you can see the logs of which methods get fired. This experiment would suggested that finish() does indeed also call onPause() and onStop(). Here is the output I get:

    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onCreate
    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onStart
    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onResume
    2170-2170/? D/LIFECYCLE_DEMO﹕ User just clicked button to initiate finish() 
    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onPause
    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onStop 
    2170-2170/? D/LIFECYCLE_DEMO﹕ INSIDE: onDestroy
    
    package com.mvvg.apps.lifecycle;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
    public class AndroidLifecycle extends Activity {
    
        private static final String TAG = "LIFECYCLE_DEMO";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "INSIDE: onCreate");
            setContentView(R.layout.activity_main);
            LinearLayout layout = (LinearLayout) findViewById(R.id.myId);
            Button button = new Button(this);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    Toast.makeText(AndroidLifecycle.this, "Initiating finish()",
                            Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "User just clicked button to initiate finish()");
                    finish();
                }
    
            });
    
            layout.addView(button);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.d(TAG, "INSIDE: onStart");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.d(TAG, "INSIDE: onStop");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.d(TAG, "INSIDE: onDestroy");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.d(TAG, "INSIDE: onPause");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.d(TAG, "INSIDE: onResume");
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:35

    My 2 cents on @K_Anas answer. I performed a simple test on finish() method. Listed important callback methods in activity life cycle

    1. Calling finish() in onCreate(): onCreate() -> onDestroy()
    2. Calling finish() in onStart() : onCreate() -> onStart() -> onStop() -> onDestroy()
    3. Calling finish() in onResume(): onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()

    What I mean to say is that counterparts of the methods along with any methods in between are called when finish() is executed.

    eg:

     onCreate() counter part is onDestroy()
     onStart() counter part is onStop()
     onPause() counter part is onResume()
    
    0 讨论(0)
提交回复
热议问题