Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I forward Intent parameters through chains of Activities?

I have lots of Activities chained together with Intents and some Intents require parameters passed in the extras Bundle. When I have to forward parameters through multiple Activities, should I copy each one explicitly or is there a best-practice way of doing it? For instance, I could clone-copy the current Intent as a starting point for calling other subtask Intents, and this would (presumably) copy all previous Bundle parameters.

As an illustration, say you have a file explorer Activity that is in one of two modes: Expert and Novice. You want to pass this state to some subtask Activity like a file properties page, which you could do by calling putExtra("skillLevel", "Expert") on the Intent before you launch it. Now if the property page also has a subtask Activity, compression options for instance, how should you forward on the "skillLevel" parameter?

like image 347
Rupert Rawnsley Avatar asked Dec 28 '11 20:12

Rupert Rawnsley


People also ask

What are the parameters of intent?

Intent parameters are used to identify and extract values within training phrases. These are specific words or phrases you want to collect from the user. Parameter name: The name associated with the parameter. This is used to reference the parameter in slot filling for scenes.

Which of the following code will you use to pass data to the sub activities of Android?

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


3 Answers

I dont know why you would want to copy all the other properties using a constructor.

newIntent.putExtras(oldIntent);

Should do the trick.

like image 59
Gary Avatar answered Oct 09 '22 04:10

Gary


I think the best and cleaner way to do it is to initialize the next Intent using the received Intent through the Intent(Intent) constructor.

Intent newIntent = new Intent(receivedIntent);
like image 43
Cristian Avatar answered Oct 09 '22 03:10

Cristian


If a parameter is system wide, it may be easier to store it in Shared Preferences until it is changed (such as difficulty of a game). It would have the side effect of remembering the set difficulty when the user leaves the app.

like image 23
Ashterothi Avatar answered Oct 09 '22 04:10

Ashterothi