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
1
vote
0 answers

How to resolve -Undefined is not an object (evaluating 'i.View.propTypes.style') in React native

It has been couple of days since I am facing this issue, after researching for a while I thought I would ask here in case any one could help. I have a react native app running currently on Android and it comes up with the following error. Undefined…
BRDroid
  • 3,920
  • 8
  • 65
  • 143
1
vote
1 answer

Sending a MultiMap type datastructure through an intent in android studio

I had to create a "multimap" type data structure for my android app that looks like this HashMap sampleStorage = new HashMap I am trying to pass it through several activities but it fails to pass to the…
1
vote
1 answer

Store Bundle with extras in MainActivity

As a novice Android/Java developer, I hope this is not a trivial question. I've been struggling with it for hours now though, so I hope to get some pointers. I am actually developing an alarm/timer application with multiple intervals for a specific…
reynard80
  • 105
  • 2
  • 6
1
vote
2 answers

Bundle not cleared

I have weird problem. How a Bundle can not be cleared after calling its method clear() This is my code : if(isFromPushNotif) { Bundle pushNotifBundle = getIntent().getExtras() ; Log.i("SplashScreen","…
Imene Noomene
  • 3,035
  • 5
  • 18
  • 34
1
vote
3 answers

Pass values from activity to Fragment by bundle

in my fragment the bundle is always null main activity public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …
pinale
  • 2,060
  • 6
  • 38
  • 72
1
vote
2 answers

How to send data from adapter into activity in Android

I want send data from adapter into activity but without startActivity. I write below codes in adapter : Intent intent = new Intent(context, MainActivity.class); intent.putExtra("sendDate", model.get(0).getLastSaleDate()); And write below codes in…
user8022871
1
vote
1 answer

Require an extra to be passed to activity

I am creating an activity that I wish to require a specific extra in the intent bundle. I am wondering what the best practice would be for such. The plan is to not start, or immediately finish the activity if the bundle does not include a specific…
erik
  • 4,946
  • 13
  • 70
  • 120
1
vote
0 answers

store intent in bundle on onSaveInstanceState / Class not found when unmarshalling

I'm storing intent in bundle on onSaveInstanceState. But I have a problem while getParcelable on onCreate. I got ClassNotFoundException. If I try to putParcelable with MyParcel alone, I do not get Exception, but If I put it in intent and pass it…
1
vote
3 answers

How to pass custom object arraylist from activity to fragment with instance

I am using viewpager and creating fragments and i want to pass the arraylist. So i have tried the below things: MainActivity: private ArrayList mArrayList = null; ViewPagerAdapter adapter = new…
user3997016
1
vote
2 answers

how can pass data between fragment to adapter

see all post about this and understand in all cases people explain about pass data from adapter to fragment with interface and i use that but my question is how can pass data from fragment to adapter ? and how can pass value of data from those? this…
Erfan
  • 3,059
  • 3
  • 22
  • 49
1
vote
1 answer

Is it Possible to take Picture (without save to external or internal storage) and Crop it?

I post my code here, which, First save the captured image to the local directory and Fetch it from directory and crop. I need to CAPTURE THE IMAGE AND IMMEDIATELY CROP IT THEN SAVE. Is it possible to save the captured image in Bundle/Intent Extra…
BraveHeart
  • 117
  • 1
  • 1
  • 10
1
vote
1 answer

Pending intent extras are not cleared even if activity is re created

I have widget in which I have speak button. On pressing speak button on widget I am opening MainActivity which is declared single top in manifest. Following is code on speak widget button Intent speakIntent = new Intent(context,…
1
vote
2 answers

SavedInstanceState is not null but is empty

I have a ViewPager with some fragments where the user enters different inputs. One of these fragments contains a DatePicker and a TimePicker which I store (when user has selected a date and time) as a Joda LocalDate and LocalTime. I want to save…
1
vote
1 answer

bundle.getString() return null value

I want to pass a String between two activities, but when I receive the String, I get a null value. I use the same code in other activities and works perfectly. Any suggestion? First activity: protected void onCreate(Bundle savedInstanceState) { …
Dani
  • 29
  • 5
1
vote
2 answers

getParcelableArrayExtra null pointer

I am using Parcelable for a custom Song object I have in my class. However, when ever I am trying to get an arraylist extra of type Song I always get a null pointer exception. I have no problem getting string extras from the intent but when I try to…
osharifali
  • 31
  • 1
  • 6