如题,在安卓各生命周期内调用Activity.finish()方法后会经历哪些生命周期呢?
代码胜于雄辩,通过如下最简单的代码来验证,在不同生命周期插入finish方法得到后面的结果。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finish();
Log.i("hi","onCreate");
}
@Override
protected void onStart()
{
super.onStart();
Log.i("hi","onStart");
}
@Override
protected void onResume()
{
super.onResume();
Log.i("hi","onResume");
}
@Override
protected void onPause()
{
super.onPause();
Log.i("hi","onPause");
}
@Override
protected void onStop()
{
super.onStop();
Log.i("hi","onStop");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i("hi","onDestroy");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.i("hi","Restart");
}
}
1.在onCreate方法中执行:
2.在onStart方法中执行:
3.在onResume方法中执行
4.在onPause方法中执行
5.在onStop方法中执行
发现其结果跟onPause是一样的,不同的是在onPause方法执行时按Home键会直接连续调用onPause、onStop、onDestroy,而在onStop中调用的时候按Home键只会先调用onPause、onStop,只有将它从最近任务中移除时才会调用onDestroy