13

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 (i.e. no extras) from the onNewIntent() function.

Intent intent = getIntent();

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

    loadDetail(intent.getStringExtra("objectId"), "2w232");
}
4
  • Is onNewIntent method called? What launchMode have you specified for SliderMenuActivity? Mar 11, 2013 at 16:46
  • Hi, yes onNewIntent() is called. launchMode is singelTask.
    – jim
    Mar 11, 2013 at 16:48
  • Can you then post your onNewIntent method? Mar 11, 2013 at 17:01
  • Hey @conor did you ever get an answer to this which worked? I have the same issue now
    – Kibi
    May 2, 2016 at 11:18

5 Answers 5

14

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.

2
  • 1
    Same thing, when debugging I was checking both intents. They were both empty.
    – jim
    Mar 11, 2013 at 17:27
  • hmm.. I've never faced this problem and I can't find any useful information on google.. =( Mar 11, 2013 at 17:50
2

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.

1

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.

5
  • Ahmed, I don't think you've quite understood how Tasks and the Back stack work in Android, so your answer's not really correct. Have a read about how they work here: developer.android.com/guide/components/… Mar 11, 2013 at 17:00
  • Whether the activity is already running or not also doesn't affect whether the extras get delivered, so again, I think you've misunderstood how it all works Mar 11, 2013 at 17:01
  • Yes. but it does affect weather the intent gets delivered, does it not? Mar 11, 2013 at 17:03
  • Not in my experience, no. The intent is just the method of calling the activity, whether it gets re-used or not, so the information is still attached to it. If the intent wasn't used, how would Android know which activity you wanted to open? Mar 11, 2013 at 17:06
  • Yes, after reading the docs again, that seems absolutely correct. Mar 11, 2013 at 17:18
1

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

0

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;
};
1
  • please add tags. the question is not well explained. neither is clear what is the programming language.
    – JC Lizard
    Feb 3, 2014 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.