Clearing Intent

Clearing intent

UPDATE:

I didn't realise this answer would be referred to so much when I first wrote it more then 5 years ago!

I'll clarify to point out that as per @tato-rodrigo answer this won't help you detect an already handled intent in some situations.

Also I should point out I put "clear" in quotes for a reason - you are not really clearing the intent by doing this, you're just using the removal of the extra as a flag that this intent has been seen by the activity already.


I had exactly the same issue.

Above answer put me on the right track and I found even simpler solution, use the:

getIntent().removeExtra("key"); 

method call to "clear" the Intent.

Its a bit late answering since this was asked a year ago, but hopefully this helps others in the future.

Android - Clearing intent

Just finish your DoExercise activity here

fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();

}
});

and start from ReadyForNext activity like you're doing.

Now you have to do some changes in DoExercise class because your intent putExtra key change with different class otherwise you'll get null pointerException

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}

if (text != null)
textView.setText(text);

}

Hope this will help you.

How to clear Intent that started Activity?

I had similar problem. This helped me.
Maybe you have to also use onSaveInstanceState(Bundle outState) and add extra data to the bundle so inState is not null, not sure.

Intent activityIntent = null; // Subsequent times it's null

@Override
protected void onCreate(Bundle inState) {
super.onCreate(inState);
.
.
if (inState!=null) {
/*Recreating activity with a saved state*/
} else {
/*Creating activity*/
activityIntent = getIntent(); // First time through intent is used
/*Get your Extra Intent data here. You will be capturing data on 1st creation. */
}

Clear intent when navigating from activity B to activity A

getIntent() will always return the Intent that the Activity was launched with. Why not just set a flag in your MainActivity when you call startActivity() for the other Activity. When it returns to your Activity you can check the flag to determine the state and act accordingly.

Alternatively you can use startActivityForResult() which will allow you to pass information back from the Activity you launched. Also the method onActivityResult() will get called on your MainActivity when the other Activity completes, so you can set internal flags or whatever to determine what to do next.

Intent data not clearing when user presses back button then recent apps button

To get around the issue I was facing, I opted to use SharedPreferences as a means to pass data between activities.

I know SharedPreferences isn't typically used for this purpose, but it solved my issue and works.

Intent extra gets cleared out from unknown reason

As suggested by android-hub I've tried using Bundle but the problem is the same. This works ("bar" is available):

    Bundle b = new Bundle();
b.putBoolean("bar", true);
intent.putExtras(b);

while this doesn't (both "xxx" and "bar" don't exist):

    Bundle b = new Bundle();
b.putSerializable("xxx", new Serializable() {});
b.putBoolean("bar", true);
intent.putExtras(b);

The mentioned answer explains a bit what is going on, however as you see suggested solution (with Bundle) doesn't work. Anyway I guess this part is a correct explanation:

if a core OS process needs to modify the Intent extras, that process winds up trying to recreate your Serializable objects as part of setting up the extras Bundle for modification. That process does not have your class and so it gets a runtime exception.

And after reading that I saw this in logs:

W/Intent: Failure filling in extras java.lang.RuntimeException:
Parcelable encountered ClassNotFoundException reading a Serializable
object (name = test.edu.B$1)

Previously I missed that because I was only looking at logs from my app. Anyway I will probably have to decompose MySerializableObject to native types (Strings, int arrays etc) create special keys for them and etc, which I guess will work but is painful. I don't want to use Parcelable (even if it would work) because I don't want my model classes to be Android specific.

The only thing I can say after wasting couple hours of your and my time to identify this problem is that Google made really crappy design in that particular case. In my opinion PendingIntent should check if some serializable is added to intent and throw some unsupported exception already in my app or there shouldn't be API for adding Serializable to Intent in the first place if it can't be handled well with other Android components.



Related Topics



Leave a reply



Submit