通過這個練習知道, 原來按下HOME按鈕離開應用程式, 和按下BACK按鈕或調用finish()方法離開應用程式是不相同的.
按下HOME按鈕離開應用程式, 會順序調用: OnPause() -> OnStop().
再次進入應用程式會調用: OnReStart() -> OnStart() -> OnResume().
按下BACK按鈕或調用finish()方法離開應用程式: OnPause() -> OnStop() -> onDestroy().
再次進入應用程式會調用: OnCreate() -> OnStart() -> OnResume().
package
com.AndroidLifeCycle;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public
class
AndroidLifeCycle
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button finish = (Button)findViewById(R.id.finish);
finish.setOnClickListener(
new
Button.OnClickListener(){
@Override
public
void
onClick(View v) {
finish();
}});
Toast.makeText(
this
,
"onCreate"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
Toast.makeText(
this
,
"onDestroy"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onPause() {
super
.onPause();
Toast.makeText(
this
,
"onPause"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onRestart() {
super
.onRestart();
Toast.makeText(
this
,
"onRestart"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onResume() {
super
.onResume();
Toast.makeText(
this
,
"onResume"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onStart() {
super
.onStart();
Toast.makeText(
this
,
"onStart"
, Toast.LENGTH_LONG).show();
}
@Override
protected
void
onStop() {
super
.onStop();
Toast.makeText(
this
,
"onStop"
, Toast.LENGTH_LONG).show();
}
}
沒有留言:
張貼留言