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
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
  • 11
  • 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
  • 676
  • 2
  • 11
  • 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
  • 97
  • 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
  • 3
  • 18
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
  • 95
  • 1
  • 8
  • 28
-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
1 answer

Exception in thread "main" java.lang.UnsupportedClassVersionError - While extracting bundle apks using BundleTool in android

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/tools/build/bundletool/BundleToolMain : Unsupported major.minor version 52.0 I am trying to run bundletool.jar to extract apks from .aab in android studio.
-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?
-1
votes
3 answers

How to Pass String Array from RecyclerView Adapter to Fragment in Android

I am retrieving values from Adapter and storing Values in ArrayList.I am converting ArrayList to String Array and passing the Array from RecyclerView Adapter to Other Fragment, but I am getting null values in other Fragment. This is code for…
-1
votes
1 answer

How to Send the data in Bundle from RecyclerView Adapter, if Button is Clicked on Fragment

I have set data in RecyclerView on Fragment.I have one Button at the bottom below RecyclerView.After the Button is clicked I want to get some details from the individual card after Checkbox is checked.After the Button is clicked from Fragment I…
-1
votes
1 answer

Getting nullpointer exception sending data from activity to fragment onCreate

I've been trying to send a string of data from my activity to a fragment using bundle but when I try to access it, it gives a nullpointerexception. The oncreate method in the activity looks like this @Override protected void onCreate(Bundle…
Kevin
  • 73
  • 6
-1
votes
2 answers

How to add bundle to Android fragment

How do i add bundle i created to: int index = 0; Here is the code i am trying to add bundle. Where am i getting wrong? public class NewsItemFragment extends Fragment { public static final String KEY_NEWS_ITEM_INDEX =…
-1
votes
2 answers

Android Activity being randomly Created and Destroyed

I am not understanding how android activities are managed. I have an activity and every once in a while i have noticed that, when my app goes into the background, android destroys whatever the current activity is (say Activity3) and several other…
zdanman
  • 391
  • 1
  • 2
  • 13
1 2 3
18
19