Friday, November 3, 2017

Bundle

Bundle :

Hi Android Guys!

Today we are going to see about Bundle concept in android, Here how to create bundle and how to use in another activity.

Step 1

In First Activity,

 // Creating Bundle object

    Bundle b = new Bundle();                     

  // Storing data into bundle

     b.putString("fullname", fullname);
     b.putLong("phoneNumber", phone);
     b.putDouble("age", ageDouble);
     b.putBoolean("married", isMarried);


    // Creating Intent object

       Intent in = new Intent(AndroidBundleExample.this, AndroidSecondActivity.class); 

    // Storing bundle object into intent

        in.putExtras(b);
        startActivity(in);

Step 2

     //Second getting activity

     // get the Intent that started this Activity

        Intent in = getIntent();       
        Bundle b = in.getExtras(); 

        // getting data from bundle

        String nameString = b.getString("fullname");
        long phoneNumberLong = b.getLong("phoneNumber");
        String phoneNumberString = Long.toString(phoneNumberLong);
        double ageDouble = b.getDouble("age");
        String ageString = Double.toString(ageDouble);

No comments:

Post a Comment