I have a search screen which can be launched from clicking on a "name" field of another screen.
If the user follows this workflow, I add an extra to the Intent's Extras called "search". This extra uses the text populating the "name" field as its value. When the search screen is created, that extra is used as a search parameter and a search is automatically launched for the user.
However, since Android destroys and recreates Activitys when the screen rotates, rotating the phone causes an auto-search again. Because of this, I'd like to remove the "search" extra from the Activity's Intent when the initial search is executed.
I've tried doing this like so:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.containsKey("search")) {
mFilter.setText(extras.getString("search"));
launchSearchThread(true);
extras.remove("search");
}
}
However, this isn't working. If I rotate the screen again, the "search" extra still exists in the Activity's Intent's Extras.
Any ideas?
I have it working.
It appears getExtras() creates a copy of the Intent's extras.
If I use the following line, this works fine:
Source code of
getExtras()
The problem can be solved using extra flag which is persistent during destroys and recreations. Here is the narrowed down code:
While @Andrew's answer may provide a means for removing a specific Intent extra, sometimes it is necessary to clear ALL of the intent extras and, in this case, you will want to use
Source code of
replaceExtras
: