1

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.

Marc Quebrar Tan
  • 497
  • 2
  • 6
  • 19

4 Answers4

4

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
Orabîg
  • 11,718
  • 6
  • 38
  • 58
1

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.

Aerilys
  • 1,628
  • 1
  • 16
  • 22
1

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

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

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,...)

Alan Deep
  • 2,037
  • 1
  • 14
  • 22