I have a scenario where, after logging in through a login page, there will be a sign-out button
on each activity
.
On clicking sign-out
, I will be passing the session id
of the signed in user to sign-out. Can anyone guide me on how to keep session id
available to all activities
?
Any alternative to this case
This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
To pass data between two activities, you will need to use the Intent class via which you are starting the Activity and just before startActivity for ActivityB, you can populate it with data via the Extra objects. In your case, it will be the content of the editText.
Intent i = new Intent(this, SecondActivity. class); startActivityForResult(i, 1); In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.
The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);
In your current Activity, create a new Intent
:
String value="Hello world"; Intent i = new Intent(CurrentActivity.this, NewActivity.class); i.putExtra("key",value); startActivity(i);
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("key"); //The key argument here must match that used in the other activity }
Use this technique to pass variables from one Activity to the other.
The easiest way to do this would be to pass the session id to the signout activity in the Intent
you're using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class); intent.putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);
Access that intent on next activity:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With