Android 第三课 Activity的生命周期

 新建项目ActivityLifeCycleTest,创建主活动后,再新建两个子活动--NormalActivity和DialogActivity。

现在活动及其对应布局文件创建完毕。

编辑normal_layout.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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>

只是简单是使用了一个TextView,用来显示一行文字。

然后编辑dialog_layout.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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>

从名字上可以看出来这两个子活动一个是普通的活动,一个是对话框式的活动。现在我们来修改这个对话框式的活动,打开AndroidManifest.xml文件的<activity>标签,并修改。代码如下:

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

这里是活动的注册码,并且只给DialogActivity的代码进行了修改,给它使用了android:theme属性,这是用来给当前活动指定主题的,Android系统内置了很多主题可以选择,这里是让DialogActivity使用对话框式的主题。

接下来,重新定制主活动的布局,将里面的代码替换成如下:

<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:id="@+id/start_normal_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text = "Start NormalActivity"/>
    <Button
        android:id="@+id/start_dialog_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text= "Start DialogActivity"/>
</LinearLayout>

只是添加了两个按钮,分别对应两个子活动。

最后修改MainActivity中的代码,如下所示:

package com.example.activitylifecycletest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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 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 view) {
                Intent intent = new Intent(MainActivity.this,NormalActivity.class);
                startActivity(intent);
            }
        });
        StartDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);    
            }
        })
    }
    
    @Override
    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");
    }
}

在onCreate()方法中,我们分别为两个按钮定义了点击事件,点击按钮会启动相应的Activity,然后会在Activity的7个回调方法中分别打印了一句话,这样更好的观察理解活动的生命周期。

  • 倘若活动被回收了怎么办?

假设有一个活动A,在活动A的基础上启动了活动B,那么活动A进入了Stop状态,这时突然由于系统内存不存,将活动A回收了,然后用户按下Back键返回活动A,会出现什么情况呢?还是会正常启动A的,只不过并不是执行onReStart()方法,而是会执行活动A的onCreate()方法,因为活动A在这种情况会被重新创建一次。那么加入活动A本来是存在临时数据和状态的,那么重新创建一次,什么都不没有显示了,这不是令人很难过吗?

经查阅文档。Activity中还提供了一个onSaveInstanceState()回调方法,这个方法可以保证活动在回收之前一定被调用,因此这个方法来解决活动被回收时,临时数据得不到保存的问题。

onSaveInstanceState()方法会携带一个Bundle类型的参数,Bundle提供了一系列的方法用于保存数据,例如可以使用putString()来保存字符串,使用putInt()保存整型,每个保存方法有两个参数,一个是键,用于后面从Bundle中取值,第二个参数是真正要保存的内容。这么看来,键和保存的内容是一一对应的吧。

在MainActivity中添加如下代码:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String tempData = "Something you just typed";
        outState.putString("data_key",tempData);
    }
我们是在MainActivity文件中重写了这个方法。那么数据是保存好了,但是我们应该在哪里恢复呢?我们之前一直使用的onCreate()方法其实也有一个Bundle类型的参数。这个参数一般情况是null,但是如果在活动被回收之前有通过onSaveInstanceState()方法来保存数据的话,这个参数就会带有之前所保存的全部数据,我们只需要修改MainActivity的onCreate()方法,代码如下:
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        setContentView(R.layout.activity_main);
        if(savedInstanceState !=null){
            String tempData = savedInstanceState.getString("data_key");
            Log.d(TAG,tempData);
        }
        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 view) {
                Intent intent = new Intent(MainActivity.this,NormalActivity.class);
                startActivity(intent);
            }
        });
        StartDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);
            }
        });
    }
Intent还可以结合Bundle一起用于传输数据,首先可以把需要传递的数据都保存在Bundle对象中,然后再将Buddle对象存放在Intent里。到了目标活动之后先从Intent中取出Bundle,在从Bundle中一一取出数据。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值