12

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");
}
jim
  • 8,670
  • 15
  • 78
  • 149

5 Answers5

12

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.

Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
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.

Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
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.

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • 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: http://developer.android.com/guide/components/tasks-and-back-stack.html – Matt Taylor Mar 11 '13 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 – Matt Taylor Mar 11 '13 at 17:01
  • Yes. but it does affect weather the intent gets delivered, does it not? – Ahmed Aeon Axan Mar 11 '13 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? – Matt Taylor Mar 11 '13 at 17:06
  • Yes, after reading the docs again, that seems absolutely correct. – Ahmed Aeon Axan Mar 11 '13 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

Community
  • 1
  • 1
rodolk
  • 5,606
  • 3
  • 28
  • 34
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;
};
feri
  • 69
  • 2
  • please add tags. the question is not well explained. neither is clear what is the programming language. – JC Lizard Feb 03 '14 at 22:36