Redirection

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

主要相关代码:
Java代码

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进行重新排序的效果。

主要相关代码:
Java代码

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代码:
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. 旋转屏幕前在两个文本框中分别输入内容。

Android 2.2 API Demos -- Redirection, Reorder Activity and Save&Restore State_第1张图片

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

Android 2.2 API Demos -- Redirection, Reorder Activity and Save&Restore State_第2张图片

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

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

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

更多相关文章

  1. Android 2d物理引擎Box2d的使用示例
  2. Android 图片对比(图片相似度)代码
  3. android 代码优化:关闭输出日志
  4. [读书笔记]布局的屏幕适配常用方法

随机推荐

  1. 在Android Studio 中导入opencv2.4.9
  2. Beginning Android Debug
  3. android 之 在Activity之间传递数据(使用
  4. 使用RenderScript库,在某些手机或Android
  5. android > 设置button 背景颜色
  6. Android(安卓)NDK开发之旅36--FFmpeg音视
  7. linux android V4l2 的一些精品文章连接
  8. (转帖)Android重力感应Demo
  9. [转]Android Market 链接的生成与分享
  10. Android自学笔记(番外篇):全面搭建Linux环境