活动生命周期法

时间:2017-10-24 09:21:08

标签: android

命名当另一个Activity进入前台时以及当前活动来到前台时调用的方法。 (两种情况下的方法都涉及当前活动)

1 个答案:

答案 0 :(得分:0)

通过这一步,您将了解活动生命周期。

public class MainActivity extends Activity {
String msg = "Android : ";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.d(msg, "The onCreate() event");
}

/** Called when the activity is about to become visible. */
@Override
protected void onStart() {
  super.onStart();
  Log.d(msg, "The onStart() event");
}

/** Called when the activity has become visible. */
@Override
protected void onResume() {
  super.onResume();
  Log.d(msg, "The onResume() event");
}

/** Called when another activity is taking focus. */
@Override
protected void onPause() {
  super.onPause();
  Log.d(msg, "The onPause() event");
}

/** Called when the activity is no longer visible. */
@Override
protected void onStop() {
  super.onStop();
  Log.d(msg, "The onStop() event");
}

/** Called just before the activity is destroyed. */
@Override
public void onDestroy() {
  super.onDestroy();
  Log.d(msg, "The onDestroy() event");
}
}