activity的生命周期:
onCreate()
onStart()
onResume()
onPause()
onDestory()
onRestart()
MainActivity:
package com.example.caohangcheng.firstactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
Log.i("MainActivity","------------->onCreate");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this,ActivityDemo.class);
startActivity(intent);
}
});
}
@Override
protected void onStart(){
super.onStart();
Log.i("MainActivity","------------->onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.i("MainActivity","------------->onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.i("MainActivity","------------->onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.i("MainActivity","------------->onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.i("MainActivity","------------->onDestroy");
}
}
ActivityDemo:
package com.example.caohangcheng.firstactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class ActivityDemo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("ActivityDemo","---------------");
}
@Override
protected void onStart(){
super.onStart();
Log.i("ActivityDemo","------------->onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.i("ActivityDemo","------------->onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.i("ActivityDemo","------------->onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.i("ActivityDemo","------------->onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.i("ActivityDemo","------------->onDestroy");
}
}
layout:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>