Android Studio: Passing Variable between Activities: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
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);..."
 
No edit summary
 
Line 1: Line 1:
// in 1 activity ->
String idValue = "hellow world";
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("ID", idValue);
startActivity(intent);
//in second Activity->
String i = getIntent().getStringExtra("ID");
==Alternatif==





Latest revision as of 02:21, 13 April 2022

// in 1 activity ->
String idValue = "hellow world";
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("ID", idValue);
startActivity(intent);
//in second Activity->
String i = getIntent().getStringExtra("ID");


Alternatif

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