In passing the value of a variable from 1st activity to 3rd activity should I use intent.putextra or should I make the variable a global variable so that I could use it in any activity.
-
1use singleton http://stackoverflow.com/a/7886046/1012284 , http://stackoverflow.com/q/8489239/1012284 – Padma Kumar Aug 27 '12 at 14:33
4 Answers
You have several options :
- implement your own Application class, and make this variable an attribute of the class
- save and get this variable in the preferences

- 11,718
- 6
- 38
- 58
-
Yes @Orabig, Application class provides Global variable concept in ANDROID. – Chintan Raghwani Aug 27 '12 at 14:28
The best way to do that is to use intent.
Nevertheless, you sometimes want a more persistant variable, or store a more complexe object. In these cases, you can use a static variable, in a singleton class for instance.

- 1,628
- 1
- 16
- 22
-
-
Static variable consumes more memory but is often easily to use for the developer. – Aerilys Aug 27 '12 at 14:28
This is definitely your decision but I suppose the user can go to Activity 2 and press back, and thus canceling the need for this variable.
The best option would be to send the value as an extra to Activity 2. It can then send it to Activity 3 if needed.
REMINDER
Do not use static variables
for Static Variables are WRONG Almost Always

- 45,786
- 16
- 89
- 106
-
why using public static variable is wrong? i think it's more easy if i use static variables rather than putextra. – Marc Quebrar Tan Aug 27 '12 at 15:20
you can pass the sesssion id to the signout activity in the intent ur using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)
Note: Make the session ID available to every activity where you want to allow the user to signout. Otherwise, you could store it in the Application object, but then you'll have to manage the state of the session (checking if it's valid before using it,...)

- 2,037
- 1
- 14
- 22