finish() and the Activity lifecycle

。_饼干妹妹 提交于 2019-11-26 21:17:19

问题


I'm learning Android programming for a class, and I have a quick question about how finish() fits into the Activity lifecycle.

When you make a call to finish(), what lifecycle callback is started? I presume it's onPause(), then onStop() and onDestroy(). Is this correct?

Really, I just want to make sure that it doesn't jump straight to onDestroy().


回答1:


You are correct. onPause, onStop, onDestroy.

Here are the docs.




回答2:


Really, I just want to make sure that it doesn't jump straight to onDestroy(). ???

NO!

but there is one exception when calling finish() result in activity lifecycle break this happens when u call finish() from onCreate() method in which case onDestroy() will be immediately called!

http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)




回答3:


Yes, it will not jump to onDestroy() skipping the onPause and onStop.

Also you might be interested in onPostResume() ,onPostPause() ,onPostCreate(),onUserLeaveHint(), etc .... These are not listed out in the activity life cycle




回答4:


It could also be very interesting for you to analyze such problems and issues. You can for example set a debuggin-breakpoint in the onPause() method and investigate the program flow from this point.

Also some print-outs can give you some helpful information.

You could for example write System.out.println("name of the method" + " called."); in each method of your activity which you think is called. (Overwrite for example onPause(), call super.onPause() and place a console print-out to see if the method is called.

You will learn a lot about the Android system doing such little investigations while you develop.




回答5:


Create a new Android App and place this in the main activity.

Then view the LogCat window (under Android's DDMS) for the outputs

Build you application the same − add all the onPause, onStop, etc. methods with outputs to the LogCat.

As your program runs you can monitor what is called and at what times.

package com.app.myapp;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MyApp extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this); 

        Button exit = new Button(this);
        exit.setText("finish");
        exit.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Log.v("MyApp", "finish");
                finish();
            }
        });

        layout.addView(exit);

        setContentView(layout);

        Log.v("MyApp", "onCreate");
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();

        Log.v("MyApp", "onDestroy");
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();

        Log.v("MyApp", "onPause");
    }

    @Override
    protected void onRestart()
    {
        // TODO Auto-generated method stub
        super.onRestart();

        Log.v("MyApp", "onRestart");
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();

        Log.v("MyApp", "onResume");
    }

    @Override
    protected void onStart()
    {
        // TODO Auto-generated method stub
        super.onStart();

        Log.v("MyApp", "onStart");
    }

    @Override
    protected void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();

        Log.v("MyApp", "onStop");
    }

}


来源:https://stackoverflow.com/questions/12655898/finish-and-the-activity-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!