Android - Activities (几种状态的切换)(2015.12.1)
2015-12-01 17:55
661 查看
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
相关文章推荐
- Android通过Url传递数据,实现分享功能
- Android学习笔记之Activity间传递参数
- Android service服务用法案例
- 打通Android、IOS、ANE制作流程
- Android(1)
- <4>Android4.2 G-Sensor工作流程
- Android加载动画
- Android getevent / sendevent的用法
- Android 各种设计模式
- android 计时器
- Android ScrollView自动滑动一段距离的问题解决
- <2>Android4.4——传感器初始化流程
- 重读《Kotlin For Android》一
- [Android] 简单的状态机实现
- Android开发笔记(七)初识Drawable
- Android——获取手机应用图标
- Android开发实践:为什么要继承onMeasure()
- android handler
- android Paint之setXfermode方法
- [Android学习笔记七] 设备管理服务示例开发