Android Studio: Passing Variable between Activities

From OnnoWiki
Revision as of 08:56, 13 April 2022 by Onnowpurbo (talk | contribs) (Created page with " In your current Activity, create a new Intent: String value="Hello world"; Intent i = new Intent(CurrentActivity.this, NewActivity.class); i.putExtra("key",value);...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


In your current Activity, create a new Intent:

String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("key",value);
startActivity(i);


Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    //The key argument here must match that used in the other activity
}


Use this technique to pass variables from one Activity to the other.


Referensi