简体   繁体   中英

Intent extras missing when activity started

Inside a broadcast receiver I want to start my app (Activity) and pass in some data.

My problem is that the extras don't seem to carry over into the activity. I am trying to get the data inside the onNewIntent(Intent i) function.

Any ideas?

Here is my current attempt in the BroadcastReceiver :

Intent intSlider = new Intent();
intSlider.setClass(UAirship.shared().getApplicationContext(), SliderMenuActivity.class);
intSlider.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intSlider.putExtra("action", ScreensEnum.Object);
intSlider.putExtra("objectId", objectId);
intSlider.putExtra("objectCode", objectCode);
intSlider.putExtra("userId", userId);

UAirship.shared().getApplicationContext().startActivity(intSlider);

EDIT - Added code used in onNewIntent() and onCreate()

The following code works great in onCreate() when the app isn't currently running. For when the app is already running the same code doesn't work (ie no extras) from the onNewIntent() function.

Intent intent = getIntent();

if(intent.hasExtra("objectId")) {

    loadDetail(intent.getStringExtra("objectId"), "2w232");
}

The problem is getIntent() method. It always returns the intent that started the activity, not the most recent one. You should use intent that was passed to onNewIntent method as an argument.

Extract from the docs

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

I think the last paragraph explains your problem.

You have to set the flag FLAG_ACTIVITY_SINGLE_TOP or set launchMode singleTop in the manifest file.

Of course when onNewIntent is called you do not use getIntent but the Intent received as argument. onNewIntent will be called when the activity instance already exists. For example, if last time you pressed the Home Screen button.

I wrote a solution that worked for me here: Intent with old extra in onCreate() for singleTask Activity

We stumbled upon this problem once, when we were trying to launch/call onNewIntent on an Activity in response to a local notification tap. The extras that we put on our Intent were disappearing at the time onNewIntent received it.

I don't remember this being documented anywhere back then, but the "problem" was that we weren't setting the action field on the Intents that we prepared. Turns out if the Intent received by your Activity doesn't have an action set using setAction , the system still delivers the Intent to its destination, but doesn't transmit the extras you have set while creating the Intent.

TL;DR:

If you encounter this problem with an Intent with no action, calling setAction to set an arbitrary action value before sending the Intent might fix it.

You can store the last received intent in a member variable (mLastIntent). Then you can use this member in your onResume() method to query for your extra data.

private Intent mLastIntent;
@Override
protected void onNewIntent(Intent intent) {
    mLastIntent = intent;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM