Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 335
Tom Avatar asked Nov 14 '12 15:11

Tom


People also ask

How do you call an intent from another intent?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i); Activities which are started by other Android activities are called sub-activities.

How do I send intent from one activity to another?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is the use of intent createChooser () method?

Most commonly, ACTION_SEND action sends URL of build-in Browser app. While sharing the data, Intent call createChooser() method which takes Intent object and specify the title of the chooser dialog. Intent. createChooser() method allows to display the chooser.

What is intent Flag_activity_new_task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .


2 Answers

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.

like image 150
Tom Avatar answered Sep 25 '22 14:09

Tom


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);
like image 26
goemic Avatar answered Sep 23 '22 14:09

goemic