API Demos 2.2 研读笔记(10)——Redirection, Reorder Activity and Save&Restore State

Redirection

 

在android中没有像JEE中专门的机制来管理forward和redirect,在这个示例中是通过简单的判断来实现重定向的效果。

 

主要相关代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.redirect_main);

        // Watch for button clicks.
        Button clearButton = (Button)findViewById(R.id.clear);
        clearButton.setOnClickListener(mClearListener);
        Button newButton = (Button)findViewById(R.id.newView);
        newButton.setOnClickListener(mNewListener);

        // Retrieve the current text preference.  If there is no text
        // preference set, we need to get it from the user by invoking the
        // activity that retrieves it.  To do this cleanly, we will
        // temporarily hide our own activity so it is not displayed until the
        // result is returned.
        if (!loadPrefs()) {
            Intent intent = new Intent(this, RedirectGetter.class);
            startActivityForResult(intent, INIT_TEXT_REQUEST);
        }
    }

 注:如果loadPrefs()返回值为null,那么系统直接跳转到RedirectGetter Activity。

 

 

Reorder Activity

 

如果我们想要已经运行的Activity重新显示在屏幕上,而不要重新create,我们可以在Intent中添加flag,其值为Intent.FLAG_ACTIVITY_REORDER_TO_FRONT。

 

Reorder Activity这个示例就是利用这个技巧达到对栈中的Activity进行重新排序的效果。

 

主要相关代码:

Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);

注:如果在Intent中添加了Intent.FLAG_ACTIVITY_REORDER_TO_FRONT并且接收Intent的Activity处于running状态,那么接收Intent的Activity就会被置于栈顶。如果同时指定了FLAG_ACTIVITY_CLEAR_TOP,那么FLAG_ACTIVITY_REORDER_TO_FRONT就会被忽略。

 

 

Save&Restore State

 

某些时候Activity在我们不知情的情况下关闭了,例如内存资源不足或者旋转屏幕,但是我们希望Activity上的信息不丢失,当我们稍后再重新启动这个Activity时关闭前的状态仍在。这就需要用到Activity state的保存和恢复机制。

 

在一个Activity被销毁之前,onSaveInstanceState(Bundle outState)会被调用,目的是保存Activity的state。

在一个Activity被重新创建时,如果ActivityThread中的savedInstanceState不为空,那么onRestoreInstanceState(Bundle savedInstanceState)会被调用,销毁前的状态会被恢复。

 

系统在设计上就尽量做到尽可能少的去调用onSaveInstanceState和onRestoreInstanceState方法。一般的Activity跳转时不需要保存和恢复状态,所以这种情况下一般不会调用(除非Activity因内存不足而被杀死)。

 

在layout中的只要是指定了id的view,它们的状态都会被保存和恢复。

 

在API Demo示例中,设置文本框的属性android:freezesText="true",同时在Activity中添加java代码:

/**
     * Retrieve the text that is currently in the "saved" editor.
     */
    CharSequence getSavedText() {
        return ((EditText)findViewById(R.id.saved)).getText();
    }

    /**
     * Change the text that is currently in the "saved" editor.
     */
    void setSavedText(CharSequence text) {
        ((EditText)findViewById(R.id.saved)).setText(text);
    }

不是指定id就可以了吗?加这些有什么目的?我不甚明白。待日后研究吧。

 

我们通过旋转屏幕来看这个示例的效果:

1. 旋转屏幕前在两个文本框中分别输入内容。



 

2. 旋转屏幕后第一个文本框中新输入的内容还在,第二个则消失了。



 注:旋转屏幕的时候当前的Activity被销毁病重建了。其间调用了onSaveInstanceState和onRestoreInstanceState方法。

 

我用debug模式跟踪查看了旋转屏幕时Activity lifecycle的具体流程:

onSaveInstanceState-->onPause-->onStop-->onDestroy-->onCreate-->onStart-->onRestoreInstanceState-->onResume

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值