Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing extras from passed-in Intent

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?

like image 905
Andrew Avatar asked Dec 23 '10 17:12

Andrew


People also ask

What is intent extras in Android?

We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

Which method helps to add extra data into intent?

putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent. EXTRA_something. A full list can be found at Intent (Android Developers).

How do you pass an activity in intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);


1 Answers

I have it working.

It appears getExtras() creates a copy of the Intent's extras.

If I use the following line, this works fine:

getIntent().removeExtra("search"); 

Source code of getExtras()

/**  * Retrieves a map of extended data from the intent.  *  * @return the map of all extras previously added with putExtra(),  * or null if none have been added.  */ public Bundle getExtras() {     return (mExtras != null)             ? new Bundle(mExtras)             : null; } 
like image 88
Andrew Avatar answered Sep 19 '22 03:09

Andrew