Rupert Rawnsley Rupert Rawnsley - 5 months ago 28
Android Question

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?

Answer

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