Tuesday, May 29, 2012

Pass Data Between Activities in Android?: Part 3

 You can Pass Data Between Activities using android.os.Bundle object which encapsulates data as key-value pairs.

   In SenderActivity.java Activtiy :



//Create new Intent Object, and specify class Intent intent = new Intent(); intent.setClass(SenderActivity.this,Receiveractivity.class); //new Bundle Object, and put data in bundle object Bundle bundle = new Bundle(); bundle.putString("senddata","This is From SenderActivity.class"); //Put Bundle object i.e bundle in intent intent.putExtras(bundle); //Call startActivity to start new Activity SenderActivity.this.startActivity(intent);


   In SenderActivity.java Activtiy :



//Intent To obtain the bundle object from SenderActivity Bundle bundle = this.getIntent().getExtras(); if(bundle !=null) { //ObtainBundleData in the object String strdata = bundle.getString("senddata"); //Do something here if data received } else { //Do something here if data not received }



Download Source Code: Intentpassdata_using_bundle








0 comments:

Post a Comment

 
Powered by Blogger