In NixMashupLinks for Android we need to persist a navigation drawer selection across multiple activities. This logic will be different after we refactor for the Material Design way of building navigation drawers, but until we do we still want to see the category of NixMashup Links we selected regardless of where we are in the app.
It turns out that persisting the position of a navigation drawer array selection is pretty straight-forward, but what turned out to be tricky was resetting the Category index and relaunching an All Records search after clicking “View All Links” from the Settings menu or “Refresh” from the Action Bar. That’s where OnNewIntent() came in.
Above we’re persisting “Java Development” across our activities, but what about setting the selection to “All Records” and removing any category filtering without relying on an OnItemClick() method to retrieve the selection through the “position” parameter for passing along in the Intent as we see in the excerpt below?
For completeness, here is how we handle the drawer array item selection in the new Activity’s onCreate() with the assistance of a NavigationDrawerHelper class.
Resetting the ArrayList
When we click an Action Bar “Refresh” button or select “View All Links” from the Settings Menu we DO NOT want to see the “Java Development” category highlighted. We also need to make sure we are retrieving all records. Two Android features will get us what we want: 1) set our main LinkListActivity class to launch in singleTop Mode and 2) add an onNewIntent() method.
There are four different activity launch modes: Standard, SingleTop, SingleTask and SingleInstance. In the default Standard mode a new activity is started for each new intent received. SingleTop is different because it will direct the intent at the existing activity instead of creating a new one. We set SingleTop Mode in the AndroidManifest.xml.
Now is where onNewIntent() comes in, because onNewIntent() is called for activities in singleTop launchMode when the activity is re-launched while at the top of the activity stack. Instead of a new instance of the activity being started, onNewIntent() is called on the existing instance with the Intent used to re-launch it.
Keywords: when the activity is at the top of the activity stack. Nice! This is where we will reset the Category Array and search filtering to “All Records” because we know we’re at the top of the stack.
In our main activity onNewIntent() we’ll first call our base class onNewIntent() which resets the Navigation Selection, because EXTRA_TAG by default will be “0”, or the array “All Records” position. Here is our base class onNewIntent().
Our Main LinkListActivity onNewIntent() will then reset the searching parameters to retrieve all records, and since its launch mode is singleTop can simply perform the query to reload the links in fragment.updateItems().
Android Coder Notes for this Post
Source code for the Navigation Drawer described in this post is located in the Repository Version 1.1 Branch.