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

Pass Boolean from Activity to not started Fragment

On startup my app loads a SignInActivity.java wherein the user can chose to Login using various social media platforms or chose to skip. This is logged using a "isLoggedIn" boolean value on the SignInActivity.java. Now a specific fragment (say…
Devansh
  • 141
  • 2
  • 15
0
votes
0 answers

Issue passing data between multiple activities using extra bundle (same key)

I have 3 activities ActivityA,ActivityB,ActivityC. I want to pass a Long from ActivityA to AtivityB to ActivityC. For that I'm using the extra bundle of the intent, and using the same key. Here is part of the code: ActivityA Intent i = new…
amp
  • 11,754
  • 18
  • 77
  • 133
0
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
  • 508
  • 1
  • 3
  • 13
0
votes
2 answers

Android -Multiple information between fragments onClick

I've read a lot but I didn't find the issue. I'm displaying my News on a ListFragment and I have a onListItemClickthat is working properly according the item clicked but I'm trying to send some variables to another fragment (like a single fragment…
FilipeOS
  • 801
  • 1
  • 11
  • 42
0
votes
2 answers

android - Use text from EditText in activity1 on button in activity2

I want the button in activity2 to show the text that was entered in EditText in activity1. This is my first app using more than one activity so I may just be trying to pass things wrong. I've tried setting the button text to the direct EditText…
0
votes
2 answers

Load multiple fragments one by one

I have to populate an activity with up to 100 different fragments to iterate through all fragments i am using a for loop, but instead of it adding a fragment one by one it adds all the fragments at once. till that happens app freezes, sometimes it…
0
votes
2 answers

Android passing parameters using Bundle is returning null

I pass a parameter to my new activity in android: Intent intent = new Intent(this,InOccasion.class); intent.putExtra("key",key); //I've checked an seen that in this activity "key" isn't null this.startActivity(intent); As I said, key in this block…
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
0
votes
1 answer

Android: Unable to get data from intent

I am trying to get a string value in onActivityForResult() from Intent value. This is how I passed the Intent Intent intent = new Intent(); intent.putExtra("state", getResources().getString(R.string.intent_skip_pin_setup)); …
Adithya.K
  • 303
  • 2
  • 15
0
votes
1 answer

How to convert an android object into a byte array and do it back?

I am using message api to send messages between a smart phone and a smart watch. As it can send only byte arrays as data, I would like to convert an object into byte array while sending and reverse the conversion while receiving. I have used the…
NewOne
  • 401
  • 5
  • 19
0
votes
1 answer

How to keep all intent String values even app is closed

I am doing one small app in that app i am saving some values in intent using putExtra and sending that values to broadcast reciever.in broadcast if sms is sent i am getting the values using getstring.its working fine only when app is open.so i need…
0
votes
3 answers

Tablayout bundle in fragment - null object reference

I've used this guide here to make the activity: http://www.androidhive.info/2015/09/android-material-design-working-with-tabs I have then added an AsyncTask. I am trying to pass Activity.java private void setupViewPager(ViewPager viewPager) { …
Edward
  • 2,291
  • 2
  • 19
  • 33
0
votes
2 answers

NullPointerException on getString with Bundle for an Android fragment

I try to get mysql db information and display the info in a fragment by clicking on an item in a navigation view, for that I use Bundle(). Here is my MainActivity : String JSON_STRING; @Override protected void onCreate(Bundle…
Jey10
  • 681
  • 10
  • 30
0
votes
1 answer

Android bundle is null after replacing fragment

I have two fragments which are VolleyFragment and AnotherFragment, setting VolleyFragment as the initial fragment. In this fragment, the bundle data is displaying ok. However, when I go to AnotherFragment, the app crashes. It says this…
0
votes
1 answer

Using intent and bundle for integers in Android

Say I wanted to pass data from one activity/class to the other involving an integer data type. Here's what I have for the MainActivity (first) class so far: @Override public void onClick(View v) { Intent i = new Intent(this,…
DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
0
votes
2 answers

Android Studio: Passing multiple values through multiple activities ONLY SHOWS ONE? :(

Hello :) so here is how my "app" is supposed to work. There are multiple activities, and in each activity, there is a question, which has answers that can be chosen with radiobuttons. It eventually gets to the final activity where a result is…