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

How to pass location as extra

I am trying to pass a android.location; to an activity as a bundled extra. the Location class implements Parcelable so i thought i could just use intent.putExtra(String name,Parcelable value) but it won't let me for some reason.. Location…
erik
  • 4,946
  • 13
  • 70
  • 120
0
votes
1 answer

android-how to save dada when activity re-created

I'm moving from activity 1 to activity 2 and send some data via intent . It's ok and no problem ,but when I leave activity 2 and then come back , the intent is empty . For solving this problem I've tried this code : @Override protected void…
0
votes
1 answer

Activity loses data when coming back from detail view

I have a ListView and each of the elements of the list has its own detail view. Each of the detail views also has its "detail view". When an item in the list is clicked, I send the id of the item to the first detail view (using an Intent) and I use…
Loolooii
  • 8,588
  • 14
  • 66
  • 90
0
votes
0 answers

Saving application state to external storage in Android

I'm working on a game for Android and had been trying to figure out how to go about saving the values to external storage and reading them back later. I was going to go about outputting all the variables into a string, writing that to a file, then…
0
votes
1 answer

Change values of Bundle parameters of addPeriodicSync()

I am using a SyncAdapter in my application in which the frequency of Periodic Sync is increased when user enters a particular activity. I wanted to know if there is a way in which I can let the application know which activity has called the…
0
votes
2 answers

How to change a value to an Activity after pressing a button in android?

(Edited to be able to compare string) I have 1 TextView, and 2 buttons, one button is "Month" the other button "Week". Im trying to change the Textview accordingly to the button pressed e.g Month,Week When i start the activity for the first time it…
Isma
  • 21
  • 4
0
votes
2 answers

Unable to start activity, trouble with my own class

I have a problem with my aplication. I have a class that I put the values correctly and works fine, I think, but when I try to get the values of my class that I have on a bundle, I have this error: 04-16 02:59:12.900: E/AndroidRuntime(1443): FATAL…
Imrik
  • 674
  • 2
  • 14
  • 32
0
votes
1 answer

How to bundle services with android project

I'm trying to use google maps in my android application. Therefore, I have 2 projects. One is my android app and another will be the google-play-services. How can I export the apk so that it is bundled together with the google-play-services?
user3300845
  • 127
  • 2
  • 11
0
votes
1 answer

Passing a string value between two different android applications?

Is it possible to be able to pass a string value between two different applications? First app: final Bundle bundle = this.getIntent().getExtras(); final String a = bundle.getString("data"); Intent i = new Intent(Intent.ACTION_MAIN); PackageManager…
0
votes
3 answers

Putting and getting DoubleArray values between Activity

I have an activity where the values are retrieved from the ResultSet and I store them in an array as: flat[i] = rs.getDouble(3); flng[i] = rs.getDouble(4); i++; Intent i = new Intent(MapID.this, map.class); Bundle bundle=new…
Sukan
  • 346
  • 1
  • 3
  • 19
0
votes
2 answers

putExtra: How should I handle large arrays? Database or split to smaller arrays?

I like to know what of following ideas makes more sentence: I have three Arrays with more than 5'000 entries. And when I try to put them into Extra, I became the FAILED BINDER TRANSACTION: Split those big arrays into smaller and give them to the…
MSeiz5
  • 182
  • 1
  • 9
  • 28
-1
votes
1 answer

How to get around passing data models to fragment arguments to avoid Transaction Too Large Exception?

I'm trying to fix TransactionTooLarge exceptions. I can't find any main culprits in onSaveInstanceState. However, when it comes to passing things to intents and bundles, I am seeing a lot of the follow type of code on a fragment. companion object…
-1
votes
1 answer

How to slide images with Viewpager in Android?

I want to slide images using Viewpager, and a number of pictures should be used. (like 30) When you swipe the screen, The images must be changed. (So you should use Viewpager.) of course i can do this by using 30 fragments for the images, but that's…
-1
votes
2 answers

getArguments().getString() in fragment returns null

In the app I'm working on I'm retrieving some data from a database. I have a tablayout with fragments on my activity and I need to push data to a specific tab. Because of the tabs I'm working with an ViewPagerAdapter; so the data goes from the…
-1
votes
1 answer

Passing Data through bundle using Intent

I want to pass data from Activity1 to Activity2 and then Data of Activity1 and Activity2 combined in Activity3. how am i supposed to do it Android Studio?