Redirection示例涉及到三个Acitivity: RedirectEnter, RedirectMain,RedirectGetter。

示例的主Activity为 RedirectEnter ,RedirectEnter 启动 RedirectMain, 而Activity 会根据某个条件来决定是否将应用的控制权传给RedirectGetter 或是保持在RedirectMain。

应用代码中使用到了Shared Preferences (在之前的示例中介绍过)。 RedirectMain 将检查某个shared preferences 值是否存在:

        // 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);
}

如果不存在,则会隐藏当前activity(RedirectMain),显示RedirectGetter.

    private final boolean loadPrefs() {
// Retrieve the current redirect values.
// NOTE: because this preference is shared between multiple
// activities, you must be careful about when you read or write
// it in order to keep from stepping on yourself.
SharedPreferences preferences = getSharedPreferences("RedirectData", 0); mTextPref = preferences.getString("text", null);
if (mTextPref != null) {
TextView text = (TextView)findViewById(R.id.text);
text.setText(mTextPref);
return true;
} return false;
}

其它用到的还有startActivityForResult 。这个例子没有什么新的知识,只是涉及到了三个Activity。演示了如何根据条件触发不同的Activity,将应用控制权Redirection到不同的Activity。

这个例子用户点击“Go”按钮,RedirectEnter 启动RedirectMain ,RedirectMain 会根据shared preferences是否有值决定是否redirect 到RedirectGetter, 第一次或是点击”Clear and Exit”后,shared preferences 中不含有text值,应用会显示RedirectGetter 来取的用户输入,此时如果用户输入并“Apply”后,RedirectGetter将在Shared Preference储存textView 的值。此后,按“Back” 退回Activity List再启动RedirectEnter,按“GO”,由于Shared Preferences中有值,RediectMain不会把应用的控制权Redirect到RedirectGetter.

最新文章

  1. html5快速入门(四)—— JavaScript
  2. 为maven插件设置参数的三种方法
  3. 关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。
  4. zookeeper集群某个follower启动失败
  5. linux 权限
  6. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
  7. C#和Javascript间互转的Xxtea加解密
  8. WEB网页插件 如何实现 选择上传图片路径 【高级问题】
  9. 指针之 *((volatile unsigned long *)(x))解析
  10. Windows下python环境变量配置
  11. 使用PHP在共享内存中存储数据集
  12. C++ 性能剖析 (四):Inheritance 对性能的影响
  13. 在内网架设一个可供外网登录的ftpserver
  14. linux安装redis(转)
  15. vagrant启动报错The following SSH command responded with a no
  16. 2_PY基本数据类型
  17. .NET大批量插入数据到Oracle
  18. spring boot 注解
  19. SQL Server 2012安装时报错,错误 0x80070422怎么解决?解决方法。
  20. TensorFlow读取CSV数据(批量)

热门文章

  1. NOIWC2019游记
  2. 资深专家深度剖析Kubernetes API Server第2章(共3章)
  3. FPGA基础学习(1) -- FFT IP核(Quartus)
  4. visio 使用技巧汇总
  5. vim 常用配置项
  6. 奇妙的 clip-path 几何图形
  7. 线段树 区间更新(更新区间[x,y]的值,再求任意区间[x,y]的和)
  8. Git for Linux and Windows
  9. LeeCode(No3 - Longest Substring Without Repeating Characters)
  10. my10_使用binlog2sql闪回DML操作