Skip to content Skip to sidebar Skip to footer

Trying To Pass Data Between Activities

Im trying to pass data between two activities using this subject: How do I pass data between Activities in Android application? with this answer: In your current Activity, create a

Solution 1:

Just pass your coins value, in your sending Activity, using:

i.putExtra("new_variable_name",coins);

Note that the second parameter is your int coins value from SharedPreferences.

To read your coins value (int value) on the receiving Activity you have to read it as an Integer, not as a String.

So, use:

privateintcoinsValue=0;

protectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundleextras= getIntent().getExtras();

 if (extras != null) {

     coinsValue = getIntent().getIntExtra("new_variable_name", 0);

}

}

And there you go, coinsValue variable has your value.

Edit: To use coinsValue anywhere in your receiving class, declare it as a field, at the beginning.

Solution 2:

As I see, you get number of coins in SharedPreferences. If you do so, you need to save it to in your Activity -

SharedPreferences.Editorprefs=this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE).edit();
prefs.putInt("key2", coins);
prefs.commit();

Post a Comment for "Trying To Pass Data Between Activities"