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
2
votes
1 answer

java.io.NotSerializableException: android.os.Bundle while trying to serialize an object that contains android bundle

I want to send an object using Messageapi from a smart watch to smart phone. Since messageapi needs byte array to send messages, I want to convert my object to a byte array. I used apache utils to serialize and deserialize. I am getting the…
NewOne
  • 401
  • 5
  • 19
2
votes
2 answers

Passing Values to ViewPager Fragment

I want to pass dogruYanlis value which is an integer array to these fragments. If I try to handle this with bundle it it sends anything. The array that I received in fragment class is always null. I think that I can't handle with bundle but I don't…
2
votes
1 answer

EditText field gets an incorrect output

So, I have this testing app where I am trying to have two fragments communicate via MainActivity. After some research, I have managed to pass data from MainFragment to FragmentTwo. However, whenever I pass in string value from the MainFragment, I…
2
votes
0 answers

How can GSON deserialize key-value pairs JSON object?

I've the following response from server: “data”: { “key1”: 11 “key2”: 89 “key3”: { “key7”: 1 “key8”: -1 “key11”: 1 }, “key5”: “test” } I've made a parcelable model: class Model implements Parcelable { …
Jumpa
  • 4,319
  • 11
  • 52
  • 100
2
votes
1 answer

Getting data from clicked notification in android

Hey guys I need help on how to get the data from my pending intent which is set using a broadcast receiver. What I want to happen is to get the data of an id when the notification is clicked which will be needed for my activity. this is how I make…
alvin valdez
  • 161
  • 3
  • 13
2
votes
0 answers

Why are all these fragments receiving the same bundle items?

I'm trying to add Fragments to a view. I have a String Array and I would like to create a new fragment for each one. Seems like a simple task. String[] items = getResources().getStringArray(R.array.myArray); for (int i = 0; i < items.length; i++)…
jb15613
  • 359
  • 4
  • 12
2
votes
2 answers

Get sms address from intent data

I need to retrieve a phone number from different type of intents. Cases are intents as: Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address","phoneNumber"); …
Lampione
  • 1,622
  • 3
  • 21
  • 39
2
votes
2 answers

Update Fragment UI with data passed from Activity

My Requirement : My MainActivity receives data from other app. MainActivity is listed as shareable. Now, I need to pass this data to a fragment in MainActivity and update the fragment's textview. In MainActivity.java : here I'm handling the received…
Nikhil
  • 6,493
  • 10
  • 31
  • 68
2
votes
1 answer

Android Contact Picker Intent always return empty extras

I have got a contact picking method that uses two extras to specify whether you are creating a new contact shortcut in my app or editing an existing one. Problem is that no matter what I do, the extras always seem to be null, causing a…
leoncvlt
  • 385
  • 2
  • 5
  • 13
2
votes
0 answers

Why does Android Bundle use PutInt(String key, int value) but Intent use PutExtra(String key, int value)?

Why aren't the put methods in Android Intent named with a primitive or class suffix such as: public Intent putExtraInt (String name, int value)? Bundle put methods have the primitive suffix: public void putInt (String key, int value) Intent,…
poetryrocksalot
  • 697
  • 2
  • 10
  • 19
2
votes
2 answers

Why can I pass a serializable with a bitmap through a Bundle but get an error when the system tries to serialize it?

So I have a class, with an image that implements serializable: public class Contact implements Serializable { ... public static final String CONTACT_KEY = "contactKey"; private transient Bitmap mImage; ... } I pass this to a…
Sam
  • 3,453
  • 1
  • 33
  • 57
2
votes
2 answers

FAILED BINDER TRANSACTION while passing Bitmap from one activity to another

I want to pass a image as a bitmap from one activity to another. And i want to know whether it is possible to do like that. Sending Activity Intent intent = new Intent(getApplicationContext(), BitmapActivity.class); Bundle b = new…
user3691107
  • 117
  • 1
  • 2
  • 5
2
votes
0 answers

How to Start Activity from IntentService and passing the Class from another class?

I want to start an activity through IntentService, but the catch is the activty name or the class name will be passed as parameter to the IntentService. Following are my code blocks... public class Runner { Context context; public…
1
vote
1 answer

How is safe args in navigation component type safe (Android)?

In a normal fragment transaction we would pass data as: Fragment fragment = new Fragment(); Bundle bundle = new Bundle(); bundle.putInt(key, value); fragment.setArguments(bundle); Isn't this type safe too? So what does it mean when we say that…
1
vote
1 answer

Data isn't completely removed from my android app

I found a very strange bug in my application. The fact is that after deleting the application, logging out of the account and deleting all data, as well as after clearing the data through the system dialog, when I enter the application I get to…