Questions tagged [android-bundle]

A mapping used to pass data between various Activities & Fragments in Android

A Bundle is a mapping from String values to various Parcelable types.

It is used in Android to store and retrieve datas inside an Activity when the orientation changed or to pass these values between two Activities. You can also used a Bundle to pass data between Activity and Fragments.

Its initialization is as follows:

Bundle b = new Bundle();  
// Put values (ex: a String):  
b.putString("value_name",value);  

Then, it is related to an Intent and pass to a new Activity with this Intent:

Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtras(b);
startActivity(intent) // launch ActivityB and pass the Bundle to it  

This map allows multiples types and retrieve its values inside the new Activity as follows:

Bundle extras = getIntent().getExtras(); 
String received_value = extras.getString("value_name");

When the device does a rotation, the Activity is destroyed and recreated. The datas can be stored to avoid to lost some informations:

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object).`

In similar fashion you can use bundles to pass data between fragment :-

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Bundle has put methods for lots of data types.

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);

See the SO question What is a "bundle" in an Android application
Also the Recreating an Activity topic from Google Documentation For more information, read the reference in Documentation: Bundle
Related tags: , , ,

302 questions
0
votes
1 answer

cast soapobject to bundle

I'm new to android app development and I'm trying to send the SoapObject which is in MainActivity to the 2nd Activity using intents, by trying to put the soapObject in a Bundle. I'm getting an error: unable to cast SoapObject to Bundle Is there…
0
votes
2 answers

Putting an image to a Bundle

How can I save an image captured by the camera in a bundle and move it to the second activity? Where am I doing wrong here? public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode,…
0
votes
1 answer

Unable to read a parcelable object within another parcelable object

I have a class for Prescriptions which has fields for Medication, Doctor, and Pharmacy. Each of those classes implement Parcelable so that they can be passed inside of a Bundle. For Medication, Doctor, and Pharmacy, I don't have any trouble.…
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
0
votes
1 answer

How to update arguments in a bundle

I'm making a minesweeper game, and i'm having problems with updating the values in a bundle. At first I set the value of the difficulty to 0 and I use the value of difficulty choose other values for the game and place it in the bundle. This is how…
user2361174
  • 1,872
  • 4
  • 33
  • 51
0
votes
4 answers

Bundle extra is returning NULL

I'm having a problems. In a class "A" I have the code: Intent cInt = new Intent(Add_Product_Page.this, CategoryListActivity.class); Bundle extra = new Bundle(); extra.putBoolean("for_result", true); startActivityForResult(cInt, GET_CATEGORY,…
0
votes
2 answers

Android : Should android bundle be used as a dto

It is a common practice to use POJO's or DTOs for modeling the data from an api or a data source. For these models, you have to right a serialization/de-serialization logic or use a library like gson to do the same. I was wondering if we can use a…
0
votes
1 answer

Is it possible to pass ProgressDialog through Bundle?

I am stuck in a situation where I want to pass a ProgressDialog object through bundle. ProgressDialog PD = new ProgressDialog(); PD.setMessage("My message"); PD.setCanceledOnTouchOutside(false); PD.show(); // some other code Bundle bundle = new…
pro_newbie
  • 336
  • 2
  • 6
  • 19
0
votes
1 answer

How do I convert a Bundle's object to json where there is no key

I have a bundle that has an object in it. normally you have key value pairs but the entire bundle is a representation of an object ie Bundle[{id=39, special_requests=, definition_id=2562}] this is a simplified version because there are a lot of…
Brian
  • 4,328
  • 13
  • 58
  • 103
0
votes
2 answers

savedInstanceState memory implications

I am working on an android project with a few other developers and a bug was raised where Instance states were not being retained on garbage collection: The actual bug reported: The app has one activity with a bunch of fragments. If "don't keep…
erik
  • 4,946
  • 13
  • 70
  • 120
0
votes
0 answers

Android : Facing issue while passing data from fragment to other fragment

This might be a very simple issue. But I'am getting difficulty to find the problem where i have done. I'am passing data from One fragment to other fragment. This is my code which i have written to pass the data. Fragment fr = new ProjectFragment(); …
user3764346
  • 129
  • 2
  • 5
  • 15
0
votes
1 answer

Android platform Bundle class experiencing internal array out of bounds exception

I am getting an array out of bounds error in an Android application, and I cannot figure out how to resolve this problem. It seems as if all the relevant checks are done. My code is as follows: private final Handler handler = new Handler() { …
0
votes
1 answer

Using static variable to share data between Activities within a package

So basically, I can't shake the fact that this seems like a bad idea, but I can't really determine why. I have Activity A and Activity B in Package 1 and Activity A makes a Volley call and creates a list of models Model X that contains some simple…
zgc7009
  • 3,371
  • 5
  • 22
  • 34
0
votes
2 answers

How to pass a CopyOnWriteArrayList to a Bundle object?

I have a data structure MyObject that implements the Parcelable interface, and I want to pass a CopyOnWriteArrayList object to a Bundle object to create a new Fragment. So I tried Bundle args = new…
Lee7355512727
  • 69
  • 2
  • 10
0
votes
4 answers

how send ArrayList contents from a class to Fragment;

I'm trying to make a Favorite list. i make a SharedPreferences class that store true values and then i need to use of all true values. it's my class : public class Baham_SharedPreferences { private SharedPreferences prefs; private static final…
Saeid
  • 2,261
  • 4
  • 27
  • 59
0
votes
2 answers

JSON data search and bundle in Android

Sorry for my bad English.I am new android. I have some problem for JSON in Android that (1.)I've been to obtain information from the Internet (JSON). How can I mark all station on googlemap on particular page? (2.1) I've been to obtain information…