Dipak Ahmedabad
Dipak Ahmedabad

Reputation: 55

How to pass data between two activities in android?

How to pass data between two activities in android?

Following is my code:-

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

Second I used SharedPreferences for that:-

For Pass Data:-

 SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences",MODE_PRIVATE);  
 SharedPreferences.Editor prefEditor = gameSettings.edit();  
 prefEditor.putString("UserName", "Guest123");  
 prefEditor.putBoolean("PaidUser", false);  
 prefEditor.commit();

For Getting Data in next Activity:-

SharedPreferences gameSettings = PreferenceManager
                                .getDefaultSharedPreferences(getBaseContext());
String s= gameSettings.getString("UserName", "Dipak");
Boolean b= gameSettings.getBoolean("PaidUser", true);

but data is not getting in next activity.

Upvotes: 0

Views: 8180

Answers (6)

Shaireen
Shaireen

Reputation: 3703

You can try this:

string dataToPass= "Hello I am activity one";
Intent intent = new Intent(this, NextActivity.class);
intent.putExtras("KeyToAccessData", dataToPass);
startActivity(intent);

And in the NextActivity, get the data like this:

String datatoCollect;
Intent intent = getIntent();
dataToCollect = intent.getStringExtra("KeyToAccessData");

If the data to pass is an object then you can check out Parcelable

Upvotes: 3

PRABEESH R K
PRABEESH R K

Reputation: 99

passing data between activity is by an intent object. You can use an object of Bundle class for attaching data to the intent object. find more about it from here http://codeglympse.blogspot.in/2012/10/passing-data-to-activity.html

Upvotes: 2

sampathpremarathna
sampathpremarathna

Reputation: 4064

This is how i did,it works for me.

  Intent in = new Intent(this, SecondWindow.class);
        Bundle bundle = new Bundle();
        bundle.putString("userName", "Name");
        bundle.putString("pwd", "password");
            in.putExtras(bundle);
        startActivity(in);

in the SecondWindow.java, onCreate method

 Bundle params = getIntent().getExtras();

        String username= params.getString("userName");
        String pwd= params.getString("pwd");

Upvotes: 0

dmon
dmon

Reputation: 30168

The reason that doesn't work is because you're using two different preferences files. Use the default shared preferences in both places and it should work. However, if you just want to pass data, that's not the way to do it, you should just use more extras in your intent.

Upvotes: 1

ingsaurabh
ingsaurabh

Reputation: 15267

For first method use following line to get value

int sessionId=getIntent().getIntExtra("EXTRA_SESSION_ID");

For getting data using SP also you use this line

SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences",MODE_PRIVATE);

Upvotes: 1

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

Why don't you use mIntent.putExtra() for passing data to another activity?

Seeing your coding,you can get your sessionId in next activity using:(assuming your sessionId as integer)

int sessionId=getIntent().getIntExtra("EXTRA_SESSION_ID");

And the way you are getting values from SharedPreferences is absolutely correct.I don't know why you are not getting them in next activity.

Upvotes: 0

Related Questions