0

As it'is written in the title I want share a variable between the activities A and C without retrieving this variable in activity B ? is it possible ?

I want to have something like that :

Intent intent = new Intent(A.this,C.class);
intent.putExtra(variableA, variableC);

... (what should I write here)

Actually My App is doing that :

Intent intent = new Intent(A.this, B.class);
startActivity(intent);

Thank you for your help

enzo
  • 301
  • 2
  • 5
  • 11
  • use shared preferences, its easy and you can get the value even from a alarm and service. – Rotary Heart Jan 05 '13 at 11:07
  • @enzo why you don't simple use Shared Preferences? See this please, http://stackoverflow.com/questions/23024831/android-shared-preferences-example If you're using the variable only in Activities A & C why pass it inside the Intent to Activity B? – Avi Levin Jan 28 '17 at 10:57

1 Answers1

0

Intent.putExtra works like an HashMap, with the key/value logic. First argument is the key. The key has to unique . If you try to store different values with the same key only the last value will be inserted.

    Intent intent = new Intent(A.this,C.class);
    intent.putExtra("variableAKey", variableA);
    intent.putExtra("variableCKey", variableC);

When the onCreate of the destination activity is invoked, you can retrieve the values in this way:

Intent intent = getIntent();
intent.getIntExtra("variableAKey"); 

if variableA is a int. Intent have a getter for the primitive type and String and serilizable/parcelable objects as well. You should refer the documentation

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thnx for your answer. Actually what i want do is just share one variable between activities A and C whithout retrieving this varible in activity B ? – enzo Jan 05 '13 at 11:32
  • the you should only retrieve inside C. – Blackbelt Jan 05 '13 at 11:36
  • i can't passe a variable from the activity A to the activity C – enzo Jan 05 '13 at 18:06
  • well in activity A i wrote this : Intent intent = new Intent(A.this,C.class); intent.putExtra("variableAKey", variableA); – enzo Jan 06 '13 at 19:25
  • Intent intent = getIntent(); String varA = intent.getIntExtra("variableAKey"); Log.d("tag",varA); – enzo Jan 06 '13 at 21:35
  • you should use getStringExtra in order to retrieve strings. Also you should read the doc when somebody lonk it you – Blackbelt Jan 07 '13 at 06:52