原文地址:http://blog.csdn.net/liuweiweiwei/article/details/55261132
Android的Activity什么时候会调用onCreate()而不调用onStart()?这是一道面试题。目前网上有关资料不多,就只有一篇知乎文章:
https://www.zhihu.com/question/49563455/answer/116926574
说是在onCreate里面添加ondestroy,本人试了是不行的。废话不多说,直接贴代码:
package com.example.dispatchevent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TAG", "onCreate");
// super.onStop();
// super.onDestroy();
finish();
btn = (Button)findViewById(R.id.btn1);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("TAG", "onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("TAG", "onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d("TAG", "onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("TAG", "onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("TAG", "onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("TAG", "onStop");
}
}
我们在onCreate()里面加了super.onStop();或super.onDestroy();,这样执行到onCreate()函数时,执行流程是这样的:onCreate -> onStop或onDestroy -> onStart -> onResume。最终还是会执行onStart。而如果加上finish(),则onCreate -> onDestroy,而不会执行onStart。
网友评论