简体   繁体   中英

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.

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

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM