1. Android using Bundle for sharing variables
Note: Using bundles to share variables in Android, Activity1 and Activity2 share a variable through bundles in the following example MyValue
sharing variables between activities is quite important point during development time of your application. This Example suppose Activity1 from where to run up other Activity2 based on your selection from SUBME Nu. You gonna share variable myvalue
From Activity1
Intent Intent = new Intent (this,myactivity2.class);
Bundle bundle = new bundle ();
Bundle.putstring ("myvalue", myvalue);
Intent.putextras (bundle);
Navigation.this.startActivity (Intent);
In Activity2
Bundle bundle = Getintent (). Getextras ();
Act2myvalue= bundle.getstring ("myvalue");
Now are your application powered to share variables between and different activities.
2.
Bundle is generally used for passing data between various activities of Android. It depends on "What type of values" want to pass "but bundle can hold all types of values and passes to the new Activit Y.
can use it like .....
New Intent (Getapplicationcontext (), secondactivity. class ); Intent.putextra ("MyKey", anyvalue); StartActivity (intent);
Now you can get the passed values by ...
Bundle Extras == extras.getstring ("MyKey");
3.
In this example, the main Activity (appropriately named ' Mainactvity ') would pass information to a sub-activity (called Sec ondaryactivity). Secondaryactivity'll allow your to alter that information and then return it to mainactivity. The bundle of information is sent back and forth between the Activity objects via an Intent object.
in order to pass the information we'll create our Intent object and do both things with it before Sending it off. first, we ' ll specify Secondaryactivity as the target by passing it into the Intent ' s CONSTRUCTOR.&NB Sp Then we'll stuff the information into it (the information are stored in a ' Bundle ' object that lives inside the intent-th E Bundle is created if you call the Putextras () method of the Intent object) . Once Our Intent are ready for go, our Mainactivity would simply launch it by passing it as a parameter into Startactivityforresult ().
When Secondaryactivity is created, it would check to see if the Intent is available. If So, it wil extract the data from the Bundle and puts it into a EditText so it can alter it. Then you can click the A button to send the it back to mainactivity. When the button was clicked, a new Intent is created with the updated information stuffed into a Bundle. Secondaryactivity then calls Setresult () which would return the information back to mainactivity.
It's important to note if you pass information around like this and you'll have to add a entry into the Androidmanife St.xml file. I always seem to forget this step. You'll have to add the following line of code into the <application> section:
<activity android:name= ". Secondaryactivity "/>
Here ' s the code ...
This is the layout of file for mainactivity (main.xml) ...
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "MAIN ACTIVITY"/> <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Click to Launch secondary Activity"/></LinearLayout>
Here's the code for the Mainactivity class ...
Packagecom.remwebdevelopment;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Button; Public classMainactivityextendsActivityImplementsView.onclicklistener {Private Final intSecondary_activity_request_code=0; PrivateEditText MEditText1; PrivateButton MButton1; //DONT forget:to Add secondaryactivity to the manifest file!!!!! /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); MEditText1=(EditText) Findviewbyid (R.ID.EDITTEXT1); MButton1=(Button) Findviewbyid (R.id.button1); Mbutton1.setonclicklistener ( This); } Public voidOnClick (view view) {if(View = =MButton1) { //Create a new intent and specify that it's target is secondaryactivity ...Intent Intent =NewIntent (Getapplicationcontext (), secondaryactivity.class); //load the intent with a key "MyKey" and assign it ' s value//To be whatever have been entered into the text field ...Intent.putextra ("MyKey", Medittext1.gettext (). toString ()); //launch the secondary activity and send the intent along with it//Note that a request code was passed in as the//secondary activity returns control to this activity,//we can identify the source of the request ...Startactivityforresult (Intent, Secondary_activity_request_code); } } //We need a handler for when the secondary activity finishes it 's work//and returns control to the This activity ...@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) { Super. Onactivityresult (Requestcode, ResultCode, intent); Bundle Extras=Intent.getextras (); Medittext1.settext (Extras!=NULL? Extras.getstring ("Returnkey"): "Nothing Returned"); }}
Here's the layout file for secondaryactivity (secondary_activity.xml) ...
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Secondary ACTIVITY"/> <EditTextAndroid:id= "@+id/txtsecondary"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <ButtonAndroid:id= "@+id/btnsecondary"Android:text= "Click to Return to Main Activity"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> </LinearLayout>
Finally, here's the code for the Secondaryactivity class ...
Packagecom.remwebdevelopment;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classSecondaryactivityextendsactivity{PrivateEditText mEditText2; PrivateButton MButton2; PrivateString mintentstring; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.secondary_activity); MEditText2=(EditText) Findviewbyid (r.id.txtsecondary); MButton2=(Button) Findviewbyid (r.id.btnsecondary); //Add the event handler for the button ...Mbutton2.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (view view) {mintentstring=Medittext2.gettext (). toString (); //Create a new intent ...Intent Intent =NewIntent (); //Add "Returnkey" as a key and assign it the value//In the textbox ...Intent.putextra ("Returnkey", Medittext2.gettext (). toString ()); //get ready-to-send the result back to the caller (mainactivity)//and put our intent into it (RESULT_OK 'll tell the caller that//We have the successfully accomplished our task:Setresult (result_ok,intent); //close this Activity ...finish (); } }); //If the activity is being resumed ...mintentstring = savedinstancestate! =NULL? Savedinstancestate.getstring ("MyKey"):NULL; //Check to see if a Bundle is. if(Mintentstring = =NULL){ //get the Bundle out of the Intent ...Bundle Extras =getintent (). Getextras (); //Check to see if "MyKey" are in the bundle, if and then assign it ' s value//to Mintentstring if not, assign ' nothing passed in ' to mintentstring ...mintentstring = Extras! =NULL? Extras.getstring ("MyKey"): "Nothing passed in"; } //Set the textbox to display mintentstring ...Medittext2.settext (mintentstring); }}
About the use of bundles in Android