Removing Additional Functions from a Passed Intent

I have a search screen that can be launched by clicking on the "name" field of another screen.

If the user follows this workflow, I add an additional instance of Intent Extras called "search". This additionally uses text that fills in the name field as its value. When the search screen is created, this additional parameter is used as the search parameter, and the search is automatically started for the user.

However, since Android destroys and recreates actions when the screen rotates, turning the phone again causes an automatic search. Because of this, I would like to remove the “search” from the “Action” action when the initial search is performed.

I tried to do it like this:

Bundle extras = getIntent().getExtras(); if (extras != null) { if (extras.containsKey("search")) { mFilter.setText(extras.getString("search")); launchSearchThread(true); extras.remove("search"); } } 

However, this does not work. If I rotate the screen again, an additional “search” still exists in the Extension Activity Intent.

Any ideas?

+48
android android-intent
Dec 23 '10 at 17:14
source share
2 answers

I have a job.

It looks like getExtras () is creating a copy of the Intent add-ons.

If I use the following line, this works fine:

 getIntent().removeExtra("search"); 

Source Code 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; } 
+114
Dec 23 '10 at 17:18
source share

The problem can be solved using an additional flag, which is constant during the destruction and recreation. Here is the narrowed code:

 boolean mProcessed; @Override protected void onCreate(Bundle state) { super.onCreate(state); mProcessed = (null != state) && state.getBoolean("state-processed"); processIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); mProcessed = false; processIntent(intent); } @Override protected void onSaveInstanceState(Bundle state) { super.onSaveInstanceState(state); state.putBoolean("state-processed", mProcessed); } protected void processIntent(Intent intent) { // do your processing mProcessed = true; } 
+7
May 06 '15 at
source share



All Articles