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
If you want to tranfer bitmap between Activites/Fragments
Activity
To pass a bitmap between Activites
And in the Activity class
Fragment
To pass a bitmap between Fragments
To receive inside the SecondFragment
Transfering Large Bitmaps
If you are getting failed binder transaction, this means you are exceeding the binder transaction buffer by transferring large element from one activity to another activity.
So in that case you have to compress the bitmap as an byte's array and then uncompress it in another activity, like this
In the FirstActivity
And in the SecondActivity
Supplemental Answer: Naming Conventions for the Key String
The actual process of passing data has already been answered, however most of the answers use hard coded strings for the key name in the Intent. This is usually fine when used only within your app. However, the documentation recommends using the
EXTRA_*
constants for standardized data types.Example 1: Using
Intent.EXTRA_*
keysFirst activity
Second activity:
Example 2: Defining your own
static final
keyIf one of the
Intent.EXTRA_*
Strings does not suit your needs, you can define your own at the beginning of the first activity.Including the package name is just a convention if you are only using the key in your own app. But it is a necessity to avoid naming conflicts if you are creating some sort of service that other apps can call with an Intent.
First activity:
Second activity:
Example 3: Using a String resource key
Although not mentioned in the documentation, this answer recommends using a String resource to avoid dependencies between activities.
strings.xml
First activity
Second activity
First Activity:
Second Activity:
Updated Note that I had mentioned the use of SharedPreference. It has a simple API and is accessible across an application's activities. But this is a clumsy solution, and is a security risk if you pass around sensitive data. It's best to use intents. It has an extensive list of overloaded methods that can be used to better transfer many different data types between activities. Have a look at intent.putExtra. This link presents the use of putExtra quite well.
In passing data between activities, my preferred approach is to create a static method for the relevant activity that includes the required parameters launch the intent. Which then provides easily setup and retrieve parameters. So it can look like this
Then you can create an intent for the intended activity and ensure you have all the parameters. You can adapt for fragments to. A simple example above, but you get the idea.
The passing of data between activities is mainly by means of an intent object.
First you have to attach the data to the intent object with the use of the
Bundle
class. Then call the activity using eitherstartActivity()
orstartActivityForResult()
methods.You can find more information about it, with an example from the blog post Passing data to an Activity.
You can use
SharedPreferences
...Logging. Time store session id in
SharedPreferences
Signout. Time fetch session id in sharedpreferences
If you don't have the required session id, then remove sharedpreferences:
That is very useful, because one time you save the value and then retrieve anywhere of activity.