android-Providing Up Navigation,Providing Proper Back Navigation
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 theandroid:parentActivityName
attribute
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 forandroid.support.PARENT_ACTIVITY
,
matching the android:parentActivityName
attribute.
<!-- 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 theup
intent containsFLAG_ACTIVITY_CLEAR_TOP
, the parent activity is brought to the top of the stack, and receives the intent through itsonNewIntent()
method. - If the parent activity has launch mode
<standard>
, and theup
intent does not containFLAG_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:parentActivityName
attribute
(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 theandroid:parentActivityName
attribute
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 forandroid.support.PARENT_ACTIVITY
,
matching the android:parentActivityName
attribute.
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 TaskStackBuilder
APIs to define each activity that should be
placed into a new back stack. Then begin the target activity by calling startActivities()
,
or create the appropriatePendingIntent
by 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 PendingIntent
that
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 PendingIntent
specifies 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 DetailsActivity
defined bydetailsIntent
). So when the DetailsActivity
starts, pressing Back navigates
backward through each of theDetailsActivity
class'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:parentActivityName
attribute
(and corresponding <meta-data>
element) as described above.
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 WebView
if it has history state:
@Override public void onBackPressed() { if (mWebView.canGoBack()) { mWebView.goBack(); return; } // Otherwise defer to system default behavior. super.onBackPressed(); }
相關文章
- android-Providing Ancestral and Temporal Navigation,Putting it All Together: Wireframing the ExampleAndroidNavigation
- android-Planning for Multiple Touchscreen Sizes,Providing Descendant and Lateral NavigationAndroidNavigation
- ABAP webdynpro的view navigation和WebUI的view navigationWebViewNavigationUI
- Jetpack 之 Navigation 初探JetpackNavigation
- HarmonyOS:Navigation元件的使用Navigation元件
- 『React Navigation 3x系列教程』之React Navigation 3x開發指南ReactNavigation
- react-navigation 使用錦囊ReactNavigation
- react-navigation圖文攻略ReactNavigation
- Jetpack Navigation----原始碼解析JetpackNavigation原始碼
- Android Jetpack Navigation基本使用AndroidJetpackNavigation
- BZOJ1481 : Navigation GameNavigationGAM
- Step 11: Back Up the Database. (69)Database
- React Navigation 的個人分析與融合ReactNavigation
- Android 官方元件 Navigation 初使用Android元件Navigation
- react navigation實現透明彈窗ReactNavigation
- react-navigation的超級大坑ReactNavigation
- Web Navigation(stack棧的運用)WebNavigation
- B. Navigation System【CF 1320】Navigation
- Avoided redundant navigation to current location: "/users"IDENavigation
- 自定義react-navigation的TabBarReactNavigationtabBar
- react-navigation路由篇之StackRouterReactNavigation路由
- ReactNative填坑之旅–Navigation篇ReactNavigation
- Basic Knowledge and System Navigation Question ----RohanNavigation
- 鴻蒙HarmonyOS實戰-ArkUI元件(Navigation)鴻蒙UI元件Navigation
- 谷歌官方元件Navigation你會用了嗎?谷歌元件Navigation
- 谷歌官方元件Navigation你瞭解了嗎?谷歌元件Navigation
- 最全面的Navigation的使用指南Navigation
- Android Jetpack 架構元件之 NavigationAndroidJetpack架構元件Navigation
- 現代前端原生路由:Navigation API前端路由NavigationAPI
- React Native——react-navigation的使用React NativeNavigation
- 由於navigation引起viewwillappear不被呼叫NavigationViewAPP
- android-Creating a Navigation DrawerAndroidNavigation
- iPhone 修改navigation bar 字型和顏色iPhoneNavigation
- Tasks of a Database Administrator : Back Up the Database (11)Database
- react-navigation 5.x 最佳實踐ReactNavigation
- Android Jetpack Navigation 深入體驗報告AndroidJetpackNavigation
- 『React Navigation 3x系列教程』之createBotReactNavigation
- Bootstrap v5 Navigation Bar 導航欄bootNavigation