问题
I have couple of activities say A , B, C. Activity A starts B , B starts C and so on. In my app I have put a navigation drawer which allows users to go back to activity A. When user goes back to activity A I have passed some flags which don't actually restart the activity but just resumes it.
intent = new Intent(activity, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Now I m trying to pass some data using bundles.
bundle.putInt("selectedTab", FEATURED_COUPONS);
intent.putExtras(bundle);
But in my activity A the bundle is always null.
if(bundle != null)
{
if(bundle.containsKey("selectedTab"))
{
int tab = bundle.getInt("selectedTab");
}
}
回答1:
You're going about things in the wrong way.
If all you want to do is put an Integer
extra into the Intent
extras then don't do this...
bundle.putInt("selectedTab", FEATURED_COUPONS);
intent.putExtras(bundle);
From the docs for putExtras(Bundle extras)
...
Add a set of extended data to the intent. The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Instead just use...
intent.putExtra("selectedTab", FEATURED_COUPONS);
This isn't the real cause of your problem however. As Sumit Uppal mentions, you should implement onNewIntent(Intent intent) in Activity
A. You can then use that to set the 'current' Intent
to be the new Intent
...
@Override
protected void onNewIntent(Intent intent) {
if (intent != null)
setIntent(intent);
}
Then in onResume()
you can use...
Intent intent = getIntent();
...and then get the Bundle
from that Intent
.
回答2:
I think you should do " if(bundle != null)" task in onNewIntent(Intent) method
回答3:
If you want pass data to an activity already created you must use startActivityForResult and override onActivityResult method in activity A.
Instead, if you create the activity again I recommend to use finish() in the activity, after the startActivity method.
来源:https://stackoverflow.com/questions/24360620/resume-old-activity-by-passing-new-data-in-bundle