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

更多相关文章

  1. Android的Fragment中onActivityResult不被调用的终极解决方案
  2. Android实现图片单点旋转缩放保存-仿百度魔图
  3. (四十一) Android(安卓)O SystemServer初探
  4. Android下2d物理引擎Box2d用法简单实例
  5. [Android] S​Q​l​i​t​e​数​据​库
  6. Android(安卓)获取屏幕的多种宽高信息的示例代码
  7. Android(安卓)在Service中调用Activity
  8. android binder 讲解之权限管理
  9. Android(安卓)Service 服务—— bindService与remoteService

随机推荐

  1. Android中的几种网络请求方式详解
  2. 对Android体系结构的理解--后续会补充
  3. android应用安全——(数据抓包)跟踪监控and
  4. 解决:android:editable is deprecated: Us
  5. Android(安卓)状态栏透明的一些小结
  6. 详解Android(安卓)TableLayout中stretchC
  7. Android读写XML(下)——创建XML文档
  8. android面试题大全 android面试题总结
  9. Unable to resolve target 'android-18'
  10. Android(安卓)ApiDemos示例解析(5):App->