package com.example.helloworld;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.util.Log;
public class MainActivity extends Activity {
String msg = "Android: ";
/** Called when the activity is first created */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
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");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(msg, "The onRestart() event");
}
/** Called just before the activity is destroyed. */
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
操作几次,可以体会上图的状态变化。
1. 启动它
12-01 04:52:11.310: D/Android:(1409): The OnCreate() event
12-01 04:52:11.310: D/Android:(1409): The onStart() event
12-01 04:52:11.310: D/Android:(1409): The onResume() event
2. 熄屏
12-01 04:52:18.150: D/Android:(1409): The onPause() event
12-01 04:52:19.030: D/Android:(1409): The onStop() event
3 再唤醒
12-01 04:52:22.428: D/Android:(1409): The onRestart() event
12-01 04:52:22.448: D/Android:(1409): The onStart() event
12-01 04:52:22.448: D/Android:(1409): The onResume() event
4. 退出
12-01 04:52:38.248: D/Android:(1409): The onPause() event
12-01 04:52:39.488: D/Android:(1409): The onStop() event
12-01 04:52:39.498: D/Android:(1409): The onDestroy() event