Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Intent intent = getIntent();

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

    loadDetail(intent.getStringExtra("objectId"), "2w232");
}
like image 517
jim Avatar asked Mar 11 '13 16:03

jim


2 Answers

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.

like image 171
Vladimir Mironov Avatar answered Oct 24 '22 23:10

Vladimir Mironov


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.

like image 30
Ahmed Aeon Axan Avatar answered Oct 24 '22 23:10

Ahmed Aeon Axan