9

Perhaps I am going about this the wrong way, but I want to respond to my Android AppWidget's click event within my own app AND launch an Activity. At the time I set the PendingIntent I have another Intent which I want to launch when clicked. My onStartCommand uses this line:

final Intent mLaunchIntent = (Intent) intent.getParcelableExtra(Widget.EXTRA_INTENT);

When I call setOnClickPendingIntent I have this line prior:

mSendingIntent.putExtra(Widget.EXTRA_INTENT, (Parcelable) mLaunchIntent);

So even though mLaunchIntent is a valid Intent in both lines, the first line is missing a great deal of data. Calling startActivity then fails because the Intent is not valid.

I am wondering if it is possible, and how, to send an Intent inside of another Intent without strictly calling putExtras because that method simple adds the extras from one Intent to the next. I'd like to keep these two separate and easily accessible.

Tom
  • 6,947
  • 7
  • 46
  • 76

4 Answers4

12

I actually figured it out, the solution was quite simple. mLaunchIntent should not be cast to Parcelable or the data gets lost.

mSendingIntent.putExtra(Intent.EXTRA_INTENT, mLaunchIntent);

That was all that I needed to send an Intent through another Intent.

GreenROBO
  • 4,725
  • 4
  • 23
  • 43
Tom
  • 6,947
  • 7
  • 46
  • 76
7

You can put an Intent to an Intent with:

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_INTENT, new Intent());

To retrieve the Intent (from within an Activity) from the intent you can do the following:

Intent intent = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
goemic
  • 1,092
  • 12
  • 24
1

Can't you use a service to parse the Intent?

Lama
  • 2,886
  • 6
  • 43
  • 59
  • 2
    I am using a Service, but that Service gets called with an Intent, in this case `mSendingIntent`. I need to pass `mLaunchIntent` alongside that Intent so that the click event then launches an Activity. – Tom Nov 14 '12 at 17:38
1

There are different ways you can pass intents / objects from source to destination and vice versa. One way of doing it without using bundles or extras would be resorting to the usual class methods with variables (getters and setters). Pass the objects using methods. Another way of doing it would be the use of class variables. Ex:

public class ClassB  extends Activity{
    public static Object myObject;
    ...
}

public class ClassA extends Activity{
    ...
    @override
    protected void onCreate(Bundle savedInstanceState){
        Object myObject = ClassB.myObject;
    }
}
Axel
  • 685
  • 5
  • 14
  • That's... one solution. But that fails in my case because of a number of issues. For one, what if the user clicked two widgets in sequence. I don't want to modify a global variable or have to set up some sort of global queue. Also, the Service and BroadcastReceiver operate on different Threads. Race conditions make this a very tricky solution. – Tom Nov 14 '12 at 17:39