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: , , ,

282 questions
360
votes
12 answers

Android: How to put an Enum in a Bundle?

How do you add an Enum object to an Android Bundle?
zer0stimulus
  • 18,516
  • 28
  • 99
  • 134
289
votes
12 answers

What is a "bundle" in an Android application

What is a bundle in an Android application? When to use it?
User
  • 5,719
  • 15
  • 46
  • 80
72
votes
6 answers

What are the IPC mechanisms available in the Android OS?

Will any one please tell me what are all the IPC mechanisms that are present in Android. To my knowledge are: Intents Binders
Suman
  • 4,021
  • 7
  • 41
  • 63
68
votes
10 answers

Print the contents of a Bundle to Logcat?

Is there an easy way to print the contents of a Bundle to Logcat if you can't remember the names of all the keys (even being able to print just the key names would be cool)?
Quasaur
  • 1,177
  • 1
  • 9
  • 27
50
votes
2 answers

Android cannot pass intent extras though AlarmManager

I am trying to put an extra message in my intent to pass to AlarmManager to be triggered at a later time. My onReceive triggers correctly but extras.getString() returns null Setup: public PendingIntent getPendingIntent(int uniqueRequestCode, String…
40
votes
2 answers

Passing objects in to Fragments

I've been working with lots of Fragments recently and have been using two distinct methods of passing in objects to the Fragments, but the only difference that I can see is that in the approach taken by FragmentOne below, the object you pass in must…
Martyn
  • 15,782
  • 24
  • 69
  • 104
39
votes
2 answers

Android save state on orientation change

I've got an Android application which maintains state regarding distance traveled, time elapsed, etc. This state I can conveniently store in an object and store a reference to that object in the Bundle when Android calls onDestroy() when the user…
Intervention
  • 516
  • 1
  • 4
  • 8
21
votes
5 answers

Passing interface to Fragment

Let's consider a case where I have Fragment A and Fragment B. B declares: public interface MyInterface { public void onTrigger(int position); } A implements this interface. When pushing Fragment B into stack, how should I pass reference of…
Niko
  • 7,663
  • 5
  • 43
  • 78
18
votes
3 answers

Kotlin extensions for Android: How to use bundleOf

Documentation says: fun bundleOf(vararg pairs: Pair): Bundle Returns a new Bundle with the given key/value pairs as elements. I tried: val bundle = bundleOf { Pair("KEY_PRICE", 50.0) Pair("KEY_IS_FROZEN", false) …
Malwinder Singh
  • 5,696
  • 11
  • 49
  • 88
18
votes
1 answer

Size limitation on attached Parcelable?

I want to know is there any size limitation on Parcelable that attached to an Intent? I Read some docs about Intents, Bundles, Parcelable again and there was nothing about size limitation. But I read some answers, that say size of attached…
hasanghaforian
  • 13,142
  • 8
  • 71
  • 144
15
votes
3 answers

Class X is not abstract and does not implement fun writeToParcel() defined in android.os.Parcelable

In my Android app, I want to add a Bundle including a Place object described below to my Intent. Since serializable was slow and not recommended, I preferred Parcelable. Althoug I use Kotlin 1.3.31, I have problems parcelizing some data classes.…
14
votes
1 answer

Does retrieving a parcelable object through bundle always create new copy?

I'm passing a parcelable object to a fragment by adding into a bundle while creating the fragment. In onc instance modification to this parcelled object reflects modification in original object and in another case it is not. I'm a little baffled by…
14
votes
8 answers

How to change the position of a spinner according to position of other spinner in two different activities

I Have two android spinner dropdown in two differnt activity.But both spinner have same data from same sourec.I want to change in position of second Activity acording to position of first activity.How to resolve this issue ?. Updated code: First…
12
votes
6 answers

Android App Bundle build error: reserved file

The new app publishing format, the Android App Bundle, is an improved way to package your app. The Android App Bundle lets you more easily deliver a great experience in a smaller app size, allowing for the huge variety of Android devices available…
Javier Marín
  • 1,886
  • 2
  • 20
  • 36
12
votes
3 answers

Resume old activity by passing new data in bundle

I have couple of activities say A , B, C. Activity A starts B , B starts C and so on. In my app I have put a navigation drawer which allows users to go back to activity A. When user goes back to activity A I have passed some flags which don't…
Vihaan Verma
  • 11,446
  • 17
  • 84
  • 120
1
2 3
18 19