page.title=Implementing Temporal Navigation parent.title=Implementing Effective Navigation parent.link=index.html trainingnavtop=true previous.title=Implementing Ancestral Navigation previous.link=ancestral.html next.title=Implementing Descendant Navigation next.link=descendant.html @jd:body
Temporal navigation is navigation to previously visited screens. Users can visit previous screens by pressing the device Back button. This user interface pattern is described further in Providing Ancestral and Temporal Navigation in Designing Effective Navigation and in Android Design: Navigation.
Android handles basic Back navigation for you (see Tasks and Back Stack for details on this behavior). This lesson discusses a number of cases where applications should provide specialized logic for the Back button.
When using fragments in your application, individual {@link android.app.FragmentTransaction} objects can represent context changes that should be added to the back stack. For example, if you are implementing a master/detail flow on a handset by swapping out fragments (thus emulating a {@link android.app.Activity#startActivity startActivity()} call), you should ensure that pressing the Back button on a detail screen returns the user to the master screen. To do so, you can use {@link android.app.FragmentTransaction#addToBackStack addToBackStack()}:
// Works with either the framework FragmentManager or the // support package FragmentManager (getSupportFragmentManager). getFragmentManager().beginTransaction() .add(detailFragment, "detail") // Add this transaction to the back stack and commit. .addToBackStack() .commit();
The activity's {@link android.app.FragmentManager} handles Back button presses if there are {@link android.app.FragmentTransaction} objects on the back stack. When this happens, the {@link android.app.FragmentManager} pops the most recent transaction off the back stack and performs the reverse action (e.g., removing a fragment if the transaction added it).
If your application updates other user interface elements to reflect the current state of your fragments, such as the action bar, remember to update the UI when you commit the transaction. You should update your user interface after the fragment manager back stack changes in addition to when you commit the transaction. You can listen for when a FragmentTransaction
is reverted by setting up an {@link android.app.FragmentManager.OnBackStackChangedListener}:
getFragmentManager().addOnBackStackChangedListener( new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { // Update your UI here. } });
If a part of your application is contained in a {@link android.webkit.WebView}, it may be appropriate for Back to traverse browser history. To do so, you can override {@link android.app.Activity#onBackPressed onBackPressed()} and proxy to the WebView
if it has history state:
{@literal @}Override public void onBackPressed() { if (mWebView.canGoBack()) { mWebView.goBack(); return; } // Otherwise defer to system default behavior. super.onBackPressed(); }
Be careful when using this mechanism with highly dynamic web pages that can grow a large history. Pages that generate an extensive history, such as those that make frequent changes to the document hash, may make it tedious for users to get out of your activity.