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

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

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

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

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

更多相关文章

  1. Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
  2. Android(安卓)matrix 控制图片的旋转、缩放、移动
  3. Android(安卓)Wifi模块分析(三)
  4. Android系统配置数据库注释(settings.db)
  5. Android中dispatchDraw分析
  6. Android四大基本组件介绍与生命周期
  7. [Android(安卓)NDK]Android(安卓)JNI开发例子 ---3 在JNI中实现o
  8. Android(安卓)Service AIDL
  9. Android调用天气预报的WebService简单例子

随机推荐

  1. Forward [To: Android Beginners ]
  2. Android -- Activity官方文档简译
  3. 记录下Android两个小细节
  4. Android(安卓)surfaceview详解
  5. Android HTTP GET/POST
  6. Android 捕捉HOME键
  7. android中Menu的使用
  8. Android 获取电池基本信息代码
  9. Android-线性布局的经典案例1-计算器
  10. eclipse Android(安卓)工程在Libs导入第