
1.当第一次调用一个Activity就会执行onCreate方法
2.当Activity处于可见状态的时候就会调用onStart方法
3.当Activity可以得到用户焦点的时候就会调用onResume方法
4.当Activity没有被销毁的时候重新调用这个Activity就会调用onRestart方法
5.当Activity被遮挡住的时候就会调用onPause方法
6.当Activity处于不可见状态的时候就会调用onStop方法
7.当Activity被销毁时会调用onDestory方法
例如:
我们新建一个主活动,一个普通活动,一个对话框活动

在主活动的视图上创建2个按钮(MainActivity.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.javascript.androidlifecircletest.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/normal_"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="normal view"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dialog view"
android:id="@+id/dialog_"/>
</LinearLayout>
MainActivity绑定按钮,并以现显式的意图切换到活动
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.normal_);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,NormalActivity.class);
startActivity(intent);
}
});
Button but2 = (Button) findViewById(R.id.dialog_);
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"start");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"resume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"pause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"stop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"restart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"destroy");
}
}
为了区别onPause和onStop,我们将Dialog活动声明为对话框活动
这只需要在manifests文件中做如下修改:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NormalActivity" />
#给DialogActivity添加如下属性android:theme
<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog">
DialogActivity.java
#这里android stdio创建活动默认继承AppCompatActivity,需要修改为Activity
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
其他NormalActivity以及xml使用stdio默认生成的就好啦~~
现在运行一下~
第一步:启动应用

#######可以看到onStart和onResume响应了
第二步:点击NORMAL_VIEW

MainActivity已经完全不可见,所以响应了onStop,这可以对比后面的Dialog_view
第三部:现在返回键返回MainActivity

第四步:点击DIALOG_VIEW

MainActivity仍然可见,并未响应onStop
第五步:返回

第六步:退出应用

网友评论