首页 / ANDROID / 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1976字,纯文字阅读大概需要3分钟。
内容图文
![【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect](/upload/InfoBanner/zyjiaocheng/1205/bdcc8b620c464250b8fb7f2b25051769.jpg)
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); returntrue; } returnfalse; }
其它用到的还有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.
原文:http://www.cnblogs.com/dongdong230/p/4310535.html
内容总结
以上是互联网集市为您收集整理的【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect全部内容,希望文章能够帮你解决【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。