Tom
Tom

Reputation: 7091

Sending Intent inside of another Intent

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.

Upvotes: 9

Views: 6133

Answers (4)

goemic
goemic

Reputation: 1339

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);

Upvotes: 7

Tom
Tom

Reputation: 7091

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.

Upvotes: 13

Axel
Axel

Reputation: 695

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;
    }
}

Upvotes: 1

Lama
Lama

Reputation: 2964

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

Upvotes: 1

Related Questions