活动——活动的生命周期

返回栈
Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈被称为返回栈(Back Stack)。

活动生存期
- onCreate():在活动第一次被创建时调用,完成活动的初始化操作,比如加载布局,和绑定事件等。
- onStart():在活动由不可见变为可见时调用。
- onResume():在活动准备和用户交互的时候调用,此时活动一定位于栈的栈顶,并且处于运行状态。
- onPause():在系统准备去启动或恢复另一个活动的时候调用。
- onStop():在活动完全不可见的时候调用。
- onDestroy():在活动被销毁之前调用,之后活动的状态将变为销毁状态。
- onRestart():在活动由停止状态变为运行状态之前调用,也就是活动被重新启动。
- 这里写图片描述


目的:体验活动的生命周期
代码:
先创建默认活动和布局名称为MainActivity.java和activity_main.xml
创建NormalActivity和DialogActivity子活动。布局文件名分别为normal_layout.xml和dialog_layout.xml。
normal_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is a normal activity"/>
</LinearLayout>

dialog_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is a dialog activity"/>
</LinearLayout>

NormalActivity和DialogActivity中代码默认不变

修改AndroidManifest.xml

       <activity android:name=".DialogActivity"
            android:theme="@style/Theme.AppCompat.Dialog"></activity>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_normal_activity"
        android:text="Start NormalActivity"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_dialog_activity"
        android:text="Start DialogActiv"/>

</LinearLayout>

MainActivity中代码:

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        setContentView(R.layout.activity_main);
        Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity);
        Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity);
        startNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, NormalActivity.class);
                startActivity(intent);
            }
        });
        startDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DialogActivity.class);
                startActivity(intent);
            }
        });
    }

    protected void onStart(){
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart");
    }
}

运行结果:
1.运行程序:
这里写图片描述这里写图片描述
2.点击第一个按钮,启动NormalActivity
这里写图片描述
这里写图片描述
3.按下Back键返回MainActivity
这里写图片描述
4.点击第二个按钮,启动DialogActivity
这里写图片描述
这里写图片描述
5.按下Back键返回MainActivity
这里写图片描述
6.在MainActivity按下Back键退出程序
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值