Use the New Compatibility Package Classes
Google's recent update to the compatibility package added two new utility classes to aid with app navigation. The first of these utilities is called NavUtils. It provides static methods for navigating up to the parent of the current activity, as declared in your application manifest. Here is an example manifest entry:
Now, in the TestActivity class, NavUtils is used to navigate up to TestParentActivity when the user presses the up button:
The second class is TaskStackBuilder. This class can be used to construct a complete back stack, with a root activity and several activities on top of it. This back stack is created with a stack of intents. Calling startActivities() will create the back stack using the intents to create each activity and pushing it onto the stack.
Google's recent update to the compatibility package added two new utility classes to aid with app navigation. The first of these utilities is called NavUtils. It provides static methods for navigating up to the parent of the current activity, as declared in your application manifest. Here is an example manifest entry:
<activity android:name=".TestActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".TestParentActivity"> </meta-data> </activity>TestParentActivity is declared as a parent of TestActivity using a metadata element.
Now, in the TestActivity class, NavUtils is used to navigate up to TestParentActivity when the user presses the up button:
@Override public boolean onOptionsItemSelected(android.view.MenuItem item) { if (item.getItemId() == android.R.id.home) { NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); }The up button uses the same resource identifier as the ActionBar home icon, android.R.id.home. In addition to navigation, the NavUtils class provides a Boolean method specifying whether the current activity should display an up button.
The second class is TaskStackBuilder. This class can be used to construct a complete back stack, with a root activity and several activities on top of it. This back stack is created with a stack of intents. Calling startActivities() will create the back stack using the intents to create each activity and pushing it onto the stack.
TaskStackBuilder tsb = TaskStackBuilder.from(this); tsb.addParentStack(this); tsb.addNextIntent(new Intent(this, TestActivity.class)); tsb.addNextIntent(new Intent(this, TestActivity.class)); tsb.addNextIntent(new Intent(this, TestActivity.class)); tsb.startActivities();This example adds three copies of the TestActivity to the current back stack. The user will have to press back three times to return to the existing activity. Using TaskStackBuilder, you can create tasks with entire back stack histories from scratch.
No comments:
Post a Comment