Question

My main activity processing an ACTION_SEND intent. I put a new record in the database based on the received data int the intent text. My problem is, that android apparently preserve this intent after orientation change, so I end up with one extra line in my DB each time I rotate my phone.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.db_list);

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            storeTextInDatabase(intent);
        }
    }

    fillListFromDB();
    registerForContextMenu(getListView());

} 

How can I achive storeTextInDatabase(intent) calls only once, after each new received intent. My first idea was that I will examine the content of savedInstanceState bundle if it is null then it is the first call of the Activity. This solution has a problem, I want to process more than one intent broadcasted during my Activity lifespan.

Était-ce utile?

La solution

try processing the Intent and then removing the extra you just processed, i.e.:

intent.removeExtra(Intent.EXTRA_TEXT);

It works for me. Tested only on 2.3 tough.

Autres conseils

I tried a lot of things. Most of them was a failure:

  • calling setIntent(null) after the processing of the intent not worked. Somehow the original intent came back after orientation change.
  • I set an extra value in the bundle of the intent. Same problem here.
  • I tried the onNewIntent() method of the Activity. It never called for me.

One thing seems working hovever. I use the onSavedInstance to set a variable of the application state bundle. It is preserved between orientation changes, but not set when I handle a new Intent.

private static final String KEY_INTENT_PROCESSED = "intent processed";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.db_list);

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    boolean alreadyProcessed = savedInstanceState == null ? false : savedInstanceState.getBoolean(KEY_INTENT_PROCESSED);

    if (Intent.ACTION_SEND.equals(action) && type != null 
            && !alreadyProcessed) {            
        if ("text/plain".equals(type)) {
            storeTextInDatabase(intent);
        }
    }

    fillListFromDB();
    registerForContextMenu(getListView());

} 

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(KEY_INTENT_PROCESSED, true);
}

In your manifest file, you can skip onCreate action for orientation change. If you change orientation, it will not create new instance. If you dont want to add that, you can keep the state as well and check that at oncreate to not process same data. Save to SharedPreferences for last saved data identifier and make sure your are not processing the same thing.

<activity
        android:name="com.microsoft.adal.LoginActivity"
        android:configChanges="orientation"
        android:label="@string/title_activity_login" >
    </activity>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top