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

Reference getting passed through bundle in fragment

I am passing data through bundle from fragment1 to fragment2. Fragment1 Code : SecondFragment secondFragment = new SecondFragment(); Bundle bundle = new Bundle(); bundle.putSerializable("SendPojo", sendPojo); …
0
votes
0 answers

Null pointer exception when passing long value from ViewPager Activity to Fragment

I've researched various topics on here (Includin the one marked as duplicate by Frankenstein) about this but can't figure out why I'm getting a Null pointer. I am new to all this as well so please be nice :) I have a main_activity that when a button…
KT79
  • 89
  • 1
  • 2
  • 10
0
votes
2 answers

Android: How to pass MULTIPLE ArrayList values and one ArrayList from one activity to another in a bundle? Not working

I have several List variables to pass from Splash to the Main activity: 1) I read somewhere that I can pass them as ArrayList from Splash to Main, and it works... i.e. I can receive only the first ArrayList variable. In my…
0
votes
1 answer

What is the purpose of importing 'android.os.Bundle' in Android App Development?

What is the purpose of importing android.os.bundlein Android App Development?
0
votes
2 answers

In a single activity pass Data from first Fragment to second Fragment Through Button Click

I have one activity page in which i have two fragments. In 1st fragment there is a listview with having Main category list with pulltorefresh feature. When I click on a list item it will show the sub category of its on 2nd fragment.Thats all how it…
0
votes
2 answers

.getExtras()' on a null object reference

I am trying to pass a few values between my activities to make a game work. However, the new activity after passing the information always returns null when I try to .getExtras(). Any thoughts? Code: package name.zerosum; import…
rcrj
  • 41
  • 1
  • 2
  • 4
0
votes
1 answer

How to fetch data for a specific category in listview?

My MainActivity have a listview with some categories,If I click a particular category in my listview,it's need to redirect to another activity,which need to have the details of that particular category. Eg: IF I select FOOD in Mainactivity,I want…
acer
  • 303
  • 1
  • 5
  • 11
0
votes
1 answer

how to open another activity when i click on listItem using SQLite Database and display in TextView using Intent in android

In my project there three java classes they are: MainActivity.java,Activity2.java and DataBaseHelper.java. with two xml layouts. now i want to pass datas from MainAcitivity to Activity2 when i click on ListItem of activity_main layout and which is…
0
votes
1 answer

Passed back bundle to mainactivity has null extras

I looked around on StackOverflow, but couldn't find something like my problem. I read about a similar problem on multiple questions, but they were all about a single extra in an intent. My problem is about a bundle of extras in the intent passed…
0
votes
1 answer

Save state of a List

How to save a state of my List onSaveInstanceState method? public class MainActivity extends AppCompatActivity { private ListView listView; private List items; @Override protected void onCreate(Bundle savedInstanceState)…
Renan Gueiros
  • 27
  • 1
  • 5
0
votes
1 answer

What happens when I call restartLoader for an AsyncTaskLoader

Per the documentation, when I call restartLoader on the LoaderManager, the Bundle is delivered to my loader. Will someone please show how I might read that Bundle on the side of the loader? I hear mention of a constructor, but I am not seeing it…
learner
  • 11,490
  • 26
  • 97
  • 169
0
votes
1 answer

Android Fragment instance variable vs Bundle arguments?

What is the difference between Android Fragment instance variables and the bundle arguments? When to use what?
Coder
  • 25
  • 5
0
votes
0 answers

android unfortunately stopped after resuming activity for a long time

I am creating a simple application. while using my application, when i click home and go to another app and work for some time like 10 to 15 minutes. When i get back to my app, its will show "unfortunately app stopped" message. And then app closes…
harikrishnan
  • 1,985
  • 4
  • 32
  • 63
0
votes
0 answers

How to add item to gridview using data from bundle

I am using this tutorial to make gridview: https://developer.android.com/guide/topics/ui/layout/gridview.html I have changed the mThumbIds from array to List and I am trying to initialize it with data that came to dataGridFragment from…
zarcel
  • 1,211
  • 2
  • 16
  • 35
0
votes
1 answer

Is it secure to pass PIN via bundle/extras in Android?

I need to ask the user to enter his app pin and confirm that pin by entering it again. I am not going to save that pin on the device and will sent it on server as soon as I get it. While implementing confirmation screen, this question came into my…
ua741
  • 1,446
  • 15
  • 28