1

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 new Intent:

 Intent i = new Intent(getApplicationContext(), NewActivity.class);

 i.putExtra("new_variable_name","value");

 startActivity(i);

Then in the new Activity, retrieve those values:

    Bundle extras = getIntent().getExtras();
  
 if (extras != null) {
     
 String value = extras.getString("new_variable_name");
   
}

My problem is , I'm not success.. I have a game contains a value called coins but it is as a sharedperferences , here is the code of the sharedpreferences: (oncreate)

prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    coins = prefs.getInt("key2", 0);

Now how do I get the amount of coins I got on the Shop(Shop.java) to buy things with them?

Community
  • 1
  • 1
DavidBalas
  • 333
  • 7
  • 21
  • Are you saving the coins value correctly, so you can read it correctly too? – joao2fast4u Jul 05 '14 at 18:08
  • @joao2fast4u Currently, the coins value is working fine, at my app, every click gains you one more coin, and there is a text that shows the coins you have, it's working fine. but not over an other activity. – DavidBalas Jul 05 '14 at 18:11
  • See my edited answer. If it helped you, please accept it/up-vote it. – joao2fast4u Jul 05 '14 at 18:35
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Omar Farhat Jul 05 '14 at 19:27
  • @KingOmar Dude, I added this link at my own question because I haven't successfully solved it with your link. p.s sry bout my English – DavidBalas Jul 05 '14 at 19:32

2 Answers2

2

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:

private int coinsValue = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = 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.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
  • But then when im trying to buy something on the shop ( coinsValue=coinsValue-500; ) it says: coinsValue cannot be resolved to a variable – DavidBalas Jul 05 '14 at 18:26
  • TY! It's working :) now I need to save it again as coins when I'm doing intent to the main page(first) :) ty! – DavidBalas Jul 05 '14 at 19:15
0

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

SharedPreferences.Editor prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE).edit();
prefs.putInt("key2", coins);
prefs.commit();
Mike
  • 2,547
  • 3
  • 16
  • 30