Home > DeveloperSection > Forums > Examples of Intent and Bundles
Allen Scott

Total Post:36

Points:254
Posted on    January-18-2016 5:51 AM

 Android Android  Android Intent 
Ratings:


 1 Reply(s)
 257  View(s)
Rate this:
I need a simple example code that will show me the difference between Intents and Bundles 


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    January-18-2016 6:04 AM

In MainActivity :
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

In OtherActivity :
public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String yourDataObject = null;
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
      yourDataObject = extras.getString(KEY_EXTRA);
  }
  if (yourDataObject != null) {
        // do stuff
  } else {
      throw new IllegalArgumentException("Activity cannot find  extras " + KEY_EXTRA);
  }
}

Don't want to miss updates? Please click the below button!

Follow MindStick