EXAMPLES OF INTENT AND BUNDLES

Allen Scott

Total Post:46

Points:324
Posted by  Allen Scott
 546  View(s)
Ratings:
Rate this:
I need a simple example code that will show me the difference between Intents and Bundles 
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Examples of Intent and Bundles

    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);
      }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!