android-Providing Up Navigation,Providing Proper Back Navigation
2016-01-31 11:45
471 查看
All screens in your app that are not the main entrance to your app (the "home" screen) should offer the user a way to navigate to the logical parent screen in the app's hierarchy
by pressing the Up button in the action
bar.
>Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the
in the
If your app supports Android 4.0 and lower, include the Support Library with your app and add a
inside the
matching the
>
> If the target parent activity is in the task's back stack, it is brought forward. The way
it is brought forward depends on whether the parent activity is able to handle an
If the parent activity has launch mode
or the
the parent activity is brought to the top of the stack, and receives the intent through its
If the parent activity has launch mode
and the
the parent activity is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
You can do so by first calling
check whether the current activity instance exists in a different app's task. If it returns true, then build a new task with
Otherwise, you can use the
as shown above.
For example:
Note: In order for the
to work, you must declare the logical parent of each activity in your manifest file, using the
(and corresponding
as described above.
>Providing Proper Back Navigation
Back navigation
is how users move backward through the history of screens they previously visited. All Android devices provide a Back button
for this type of navigation, so your app should not add a Back button to the UI.
Navigation patterns that require you to manually specify the Back behavior include:
When the user enters a deep-level activity directly from a notification, an app widget, or the navigation drawer.
Certain cases in which the user navigates between fragments.
When the user navigates web pages in a WebView.
》Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the
in the
because it can determine the logical Back or Up navigation path with this information.
If your app supports Android 4.0 and lower, include the Support Library with your app and add a
inside the
matching the
Adding activities to the back stack begins upon the event that takes the user into your app. That is, instead of calling
use the
placed into a new back stack. Then begin the target activity by calling
or create the appropriate
For example, when a notification takes the user to an activity deep in your app hierarchy, you can use this code to create a
starts an activity and inserts a new back stack into the target task:
The resulting
but also the back stack that should be inserted into the task (all parents of the
backward through each of the
Note: In order for the
to work, you must declare the logical parent of each activity in your manifest file, using the
(and corresponding
Note: You should
not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when
adjusting filters). For more information, about when Back navigation is appropriate, see
the Navigation design
guide.
If a part of your application is contained in a
traverse browser history. To do so, you can override
proxy to the
by pressing the Up button in the action
bar.
>Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the
android:parentActivityNameattribute
in the
<activity>element.
If your app supports Android 4.0 and lower, include the Support Library with your app and add a
<meta-data>element
inside the
<activity>. Then specify the parent activity as the value for
android.support.PARENT_ACTIVITY,
matching the
android:parentActivityNameattribute.
>
<!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity>
> If the target parent activity is in the task's back stack, it is brought forward. The way
it is brought forward depends on whether the parent activity is able to handle an
onNewIntent()call:
If the parent activity has launch mode
<singleTop>,
or the
upintent contains
FLAG_ACTIVITY_CLEAR_TOP,
the parent activity is brought to the top of the stack, and receives the intent through its
onNewIntent()method.
If the parent activity has launch mode
<standard>,
and the
upintent does not contain
FLAG_ACTIVITY_CLEAR_TOP,
the parent activity is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
You can do so by first calling
shouldUpRecreateTask()to
check whether the current activity instance exists in a different app's task. If it returns true, then build a new task with
TaskStackBuilder.
Otherwise, you can use the
navigateUpFromSameTask()method
as shown above.
For example:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { // This activity is NOT part of this app's task, so create a new task // when navigating up, with a synthesized back stack. TaskStackBuilder.create(this) // Add all of this activity's parents to the back stack .addNextIntentWithParentStack(upIntent) // Navigate up to the closest parent .startActivities(); } else { // This activity is part of this app's task, so simply // navigate up to the logical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
Note: In order for the
addNextIntentWithParentStack()method
to work, you must declare the logical parent of each activity in your manifest file, using the
android:parentActivityNameattribute
(and corresponding
<meta-data>element)
as described above.
>Providing Proper Back Navigation
Back navigation
is how users move backward through the history of screens they previously visited. All Android devices provide a Back button
for this type of navigation, so your app should not add a Back button to the UI.
Navigation patterns that require you to manually specify the Back behavior include:
When the user enters a deep-level activity directly from a notification, an app widget, or the navigation drawer.
Certain cases in which the user navigates between fragments.
When the user navigates web pages in a WebView.
》Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the
android:parentActivityNameattribute
in the
<activity>element. This allows the system to facilitate navigation patterns
because it can determine the logical Back or Up navigation path with this information.
If your app supports Android 4.0 and lower, include the Support Library with your app and add a
<meta-data>element
inside the
<activity>. Then specify the parent activity as the value for
android.support.PARENT_ACTIVITY,
matching the
android:parentActivityNameattribute.
Adding activities to the back stack begins upon the event that takes the user into your app. That is, instead of calling
startActivity(),
use the
TaskStackBuilderAPIs to define each activity that should be
placed into a new back stack. Then begin the target activity by calling
startActivities(),
or create the appropriate
PendingIntentby calling
getPendingIntent().
For example, when a notification takes the user to an activity deep in your app hierarchy, you can use this code to create a
PendingIntentthat
starts an activity and inserts a new back stack into the target task:
// Intent for the activity to open when user selects the notification Intent detailsIntent = new Intent(this, DetailsActivity.class); // Use TaskStackBuilder to build the back stack and get the PendingIntent PendingIntent pendingIntent = TaskStackBuilder.create(this) // add all of DetailsActivity's parents to the stack, // followed by DetailsActivity itself .addNextIntentWithParentStack(upIntent) .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(pendingIntent);
The resulting
PendingIntentspecifies not only the activity to start (as defined by
detailsIntent),
but also the back stack that should be inserted into the task (all parents of the
DetailsActivitydefined by
detailsIntent). So when the
DetailsActivitystarts, pressing Back navigates
backward through each of the
DetailsActivityclass's parent activities.
Note: In order for the
addNextIntentWithParentStack()method
to work, you must declare the logical parent of each activity in your manifest file, using the
android:parentActivityNameattribute
(and corresponding
<meta-data>element) as described above.
Note: You should
not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when
adjusting filters). For more information, about when Back navigation is appropriate, see
the Navigation design
guide.
If a part of your application is contained in a
WebView, it may be appropriate for Back to
traverse browser history. To do so, you can override
onBackPressed()and
proxy to the
WebViewif it has history state:
@Override public void onBackPressed() { if (mWebView.canGoBack()) { mWebView.goBack(); return; } // Otherwise defer to system default behavior. super.onBackPressed(); }
相关文章推荐
- Android之自定义dialog对话框
- android-Creating a Navigation Drawer
- Android之Android 自定义控件与属性
- Android 在ListView中使用CheckBox,实现增加联系人的功能
- 常用的android工具类和库
- Android技巧之drawablePadding设置
- Android-框架使用之-懒惰开发-如何提升开发效率
- Android中AsyncTask解析
- Android-自定义组件-最全下拉刷新分析-推荐
- Android-内存优化-Overdraw-多度绘制
- Android手机各种截屏方法
- android项目中刷新activity界面
- Android 异步消息处理机制解析
- Android-->Dialog/DialogFragment宽度高度修改/全屏,自定义样式
- Android音乐播放器---实现Notification控制音乐播放
- Android属性动画赏析
- Android Tip : Android 获取当前Activity对象的方式:
- ubuntu需要额外安装的包用于支持android sdk的运行
- Android动画三部曲之一 View Animation & LayoutAnimation
- 自定义Dialog