您的位置:首页 > 移动开发 > Android开发

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
android: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 for
android.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 the
up
intent 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
up
intent 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: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 the
android: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 for
android.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 appropriate
PendingIntent
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 by
detailsIntent
). So when the
DetailsActivity
starts, pressing Back navigates
backward through each of the
DetailsActivity
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.
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
WebView
if it has history state:
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}

// Otherwise defer to system default behavior.
super.onBackPressed();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: