Activity--生命周期
程序员文章站
2022-07-05 08:10:09
...
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FirstActivity extends Activity { private static final String LOG_TAG="VERBOSE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.start); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } }); Log.v(LOG_TAG, "FirstActivity:onCreate"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(LOG_TAG, "FirstActivity:onDestroy"); } @Override protected void onPause() { super.onPause(); Log.v(LOG_TAG, "FirstActivity:onPause"); } @Override protected void onResume() { super.onResume(); Log.v(LOG_TAG, "FirstActivity:onResume"); } @Override protected void onRestart() { super.onRestart(); Log.v(LOG_TAG, "FirstActivity:onRestart"); } @Override protected void onStart() { super.onStart(); Log.v(LOG_TAG, "FirstActivity:onStart"); } @Override protected void onStop() { super.onStop(); Log.v(LOG_TAG, "FirstActivity:onStop"); } }
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity { private static final String LOG_TAG="VERBOSE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Button b = (Button) findViewById(R.id.back); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); Button s = (Button) findViewById(R.id.start3); s.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); startActivity(intent); } }); Log.v(LOG_TAG, "SecondActivity:onCreate"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(LOG_TAG, "SecondActivity:onDestroy"); } @Override protected void onPause() { super.onPause(); Log.v(LOG_TAG, "SecondActivity:onPause"); } @Override protected void onRestart() { super.onRestart(); Log.v(LOG_TAG, "SecondActivity:onRestart"); } @Override protected void onResume() { super.onResume(); Log.v(LOG_TAG, "SecondActivity:onResume"); } @Override protected void onStart() { super.onStart(); Log.v(LOG_TAG, "SecondActivity:onStart"); } @Override protected void onStop() { super.onStop(); Log.v(LOG_TAG, "SecondActivity:onStop"); } }
1.启动该程序,输出:
FirstActivity:onCreate
FirstActivity:onStart
FirstActivity:onResume
2.点击Start SecondActivity,输出:
FirstActivity:onPause
SecondActivity:onCreate
SecondActivity:onStart
SecondActivity:onResume
FirstActivity:onStop
3.点击Back to FirstActivity或者按BACK key,输出:
SecondActivity:onPause
FirstActivity:onRestart
FirstActivity:onStart
FirstActivity:onResume
SecondActivity:onStop
SecondActivity:onDestroy