问题
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