Friday, 20 September 2013

Android using Bundle for sharing variables

Sharing variables between Activities is quite important point during development time of your Application. This  Example suppose Activity1 from where you run up other Activity2 .  
We gonna share variable myValue.

In Activity1 :
myValue="string values";
Intent intent = new Intent(MainActivity.this,NewClass.class);
Bundle bundle = new Bundle();
bundle.putString("myValue", myValue);
intent.putExtras(bundle);
startActivity(intent); 
or

Intent intent = new Intent(MainActivity.this,NewClass.class);
intent.putExtra("myValue",AnyValue);  
startActivity(intent);

In Activity2 :
Bundle bundle = getIntent().getExtras();
String value= bundle.getString(“myValue“);
or

Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String value= bundle.getString(“myValue“);

No comments:

Post a Comment