Categories
android android-activity android-intent

How do I pass data between Activities in Android application?

1492

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

4

  • 17

    i used sharedpreference its useful also to keep login data on remeber password feature

    – shareef

    Dec 2, 2012 at 19:33

  • This works for me. stackoverflow.com/a/7325248/2125322 Thanks Darshan Computing

    – matasoy

    Sep 1, 2013 at 20:59

  • stackoverflow.com/a/37774966/6456129 may be helpful

    – Yessy

    Jul 24, 2016 at 5:48

  • for such cases try making commomUtils class with sharedprefereces Method… this will keep the code clean and related data at a place . And you will easily be able to Clear specific set of data with just one method of clearing that specific prefrencesFile,without clearing any of default app data…

    Jan 8, 2019 at 12:52


1436

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

10

  • ok if i pass session id to signout acitivity on successful login and will it work on any activity page to signout or manually i will have to assign it value on each activity??? using above procedure??

    Jan 19, 2010 at 6:57

  • 7

    Yes, you’d have to make the session ID available to every activity where you want to allow the user to signout. Alternatively, you could store it in the Application object, but then you’d have to manage the state of the session (check if it’s valid before using, etc).

    Jan 19, 2010 at 15:16

  • 1

    Please be aware that the documentation points the following: Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like “com.android.contacts.ShowAll”.

    Nov 13, 2013 at 16:25

  • 1

    And to read data from other Activity use Long session_ids=getIntent().getExtras().getLong("EXTRA_SESSION_IDS");

    – Farid

    Apr 9, 2016 at 18:39


  • 1

    How can we pass data using setData and what is difference between theses two approaches? Which one is better?

    Jul 18, 2016 at 13:04


1436

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

10

  • ok if i pass session id to signout acitivity on successful login and will it work on any activity page to signout or manually i will have to assign it value on each activity??? using above procedure??

    Jan 19, 2010 at 6:57

  • 7

    Yes, you’d have to make the session ID available to every activity where you want to allow the user to signout. Alternatively, you could store it in the Application object, but then you’d have to manage the state of the session (check if it’s valid before using, etc).

    Jan 19, 2010 at 15:16

  • 1

    Please be aware that the documentation points the following: Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like “com.android.contacts.ShowAll”.

    Nov 13, 2013 at 16:25

  • 1

    And to read data from other Activity use Long session_ids=getIntent().getExtras().getLong("EXTRA_SESSION_IDS");

    – Farid

    Apr 9, 2016 at 18:39


  • 1

    How can we pass data using setData and what is difference between theses two approaches? Which one is better?

    Jul 18, 2016 at 13:04


148

Passing Intent extras is a good approach as Erich noted.

The Application object is another way though, and it is sometimes easier when dealing with the same state across multiple activities (as opposed to having to get/put it everywhere), or objects more complex than primitives and Strings.

You can extend Application, and then set/get whatever you want there and access it from any Activity (in the same application) with getApplication().

Also keep in mind that other approaches you might see, like statics, can be problematic because they can lead to memory leaks. Application helps solve this too.

3

  • 8

    +1 for the statics problem. probably the clean up can be resolved by combining a singleton with onCreate/onTerminate method Application class.

    – Syd

    Nov 17, 2010 at 22:33

  • 12

    Hey, I know this thread was quite a while back, but the link provided is now a dead end. Is there anywhere I can find the example?

    – JuiCe

    Jun 15, 2012 at 14:38

  • @JuiCe The Android Developers blog post on memory leaks is no longer invalid.

    – Edric

    Mar 29, 2020 at 15:56