Android Activity的生命周期与加载模式超详细图文解析

  public class FirstActivity extends Activity {

  final String TAG = "-----FirstActivity-----";

  @Override

  protected void onCreate(@Nullable Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.first);

  // 输出日志

  Log.d(TAG, "-----onCreate-----");

  TextView textView = findViewById(R.id.text1);

  Button button1 = findViewById(R.id.bn_first1);

  Button button2 = findViewById(R.id.bn_first2);

  // 第一个按钮跳转到第二个Activity

  button1.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

  startActivity(intent);

  }

  });

  // 第二个按钮销毁该Activity

  button2.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  FirstActivity.this.finish();

  }

  });

  }

  @Override

  protected void onStart() {

  super.onStart();

  Log.d(TAG, "-----onStart-----");

  }

  @Override

  protected void onRestart() {

  super.onRestart();

  Log.d(TAG, "-----onRestart-----");

  }

  @Override

  protected void onResume() {

  super.onResume();

  Log.d(TAG, "-----onResume-----");

  }

  @Override

  protected void onPause() {

  super.onPause();

  Log.d(TAG, "-----onPause-----");

  }

  @Override

  protected void onStop() {

  super.onStop();

  Log.d(TAG, "-----onStop-----");

  }

  @Override

  protected void onDestroy() {

  super.onDestroy();

  Log.v(TAG, "-----onDestory-----");

  }

  }