【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
来源:互联网 发布:王宝强 知乎 编辑:程序博客网 时间:2024/06/06 10:43
示例的主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.
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
- 【起航计划 030】2015 起航计划 Android APIDemo的魔鬼步伐 29 App->Preferences->Preferences from code
- 【起航计划 029】2015 起航计划 Android APIDemo的魔鬼步伐 28 App->Preferences->Default Values 偏好默认值
- 【起航计划 006】2015 起航计划 Android APIDemo的魔鬼步伐 05 App->Activity->Custom Title 自定义标题栏
- 【起航计划 015】2015 起航计划 Android APIDemo的魔鬼步伐 14 App->Activity->Translucent Blur 模糊背景
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListene
- 【起航计划 018】2015 起航计划 Android APIDemo的魔鬼步伐 17 App->Alarm->Alarm Service
- 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
- 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller
- 【起航计划 007】2015 起航计划 Android APIDemo的魔鬼步伐 06 App->Activity->Forwarding Activity启动另外一个Activity finish()方法
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 016】2015 起航计划 Android APIDemo的魔鬼步伐 15 App->Activity->Wallpaper 系统壁纸作为当前Activity的背景
- 【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTran
- 【起航计划 014】2015 起航计划 Android APIDemo的魔鬼步伐 13 App->Activity->Translucent 半透明Activity Theme.Translucent
- 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 006】2015 起航计划 Android APIDemo的魔鬼步伐 05 App->Activity->Custom Title 自定义标题栏
- 【起航计划 007】2015 起航计划 Android APIDemo的魔鬼步伐 06 App->Activity->Forwarding Activity启动另外一个Activity finish()方法
- 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
- 【起航计划 009】2015 起航计划 Android APIDemo的魔鬼步伐 08 App->Activity->QuickContactsDemo 联系人 ResourceCursorAdapter使用 QuickContactBad
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
- 【起航计划 011】2015 起航计划 Android APIDemo的魔鬼步伐 10 App->Activity->Reorder Activities 后退栈 Intent FLAG
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstance
- 【起航计划 013】2015 起航计划 Android APIDemo的魔鬼步伐 12 App->Activity->SetWallpaper 设置壁纸 WallpaperManager getDrawingCache使用
- 【起航计划 014】2015 起航计划 Android APIDemo的魔鬼步伐 13 App->Activity->Translucent 半透明Activity Theme.Translucent
- 【React】 详解下一代开源混合应用框架Reapp
- 【Android 界面效果47】RecyclerView详解
- 【Android 界面效果48】Android-RecyclerView-Item点击事件设置
- 【Android 界面效果49】RecyclerView高度随Item自适应