Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass data between Activities in Android application?

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

like image 301
UMAR-MOBITSOLUTIONS Avatar asked Jan 19 '10 06:01

UMAR-MOBITSOLUTIONS


People also ask

How do I transfer data between android activities?

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.

How do you pass the data between two activities?

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.

How do you pass data from second activity to first activity?

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.

How can you pass data between multiple activities using intent explain?

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


2 Answers

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.

like image 53
user914425 Avatar answered Oct 06 '22 01:10

user914425


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

like image 32
Erich Douglass Avatar answered Oct 05 '22 23:10

Erich Douglass