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

How to send data from AsyncTask to Fragment

How to pass data from an activity to a fragment. It is well documented but unfortunately no solution works for me. I want to send data from an inner AsyncTask class inside my main activity to another fragment. The most popular answer is to use…
1
vote
2 answers

Android, Intents: Passed one bundle to SecondActivity

I have some class. public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent newintent = new…
1
vote
2 answers

Preserving values between activities via Bundle

I am using the following method to save the user inputted variables between activities: // Save instance between activities @Override public void onSaveInstanceState(Bundle savedInstanceState) { …
Daniel
  • 66
  • 1
  • 9
1
vote
3 answers

Better Way to Test Presence of a Value in a Bundle?

I have an activity to play a game. The user may be resuming from a saved state or initiating a new game. If resuming, the identifier for the game to resume is passed to the activity in the bundle. If it is a new game, that part of the bundle…
LDMJoe
  • 1,591
  • 13
  • 17
1
vote
0 answers

Choose from map with android marshmallow

Now I'm using android map clicklistener to allow user to choose location from map. When the click the map the chosen location latitude and longitude are printed correctly in the toast, then I send this data to another fragment through bundle. On…
1
vote
1 answer

Android Studio: Passing multiple values using intent PROVIDES ONLY 1 VALUE?

I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to…
1
vote
0 answers

What's the best way to identify a dialog caller?

I've a form fragment with more than 10 fields. Each of them, on click event, shows a number picker dialog. Now, how can I identify which field generated the value choice on dialog? In the fragment I've a single callback, due to an interface…
1
vote
2 answers

Pass String between two fragments without using an activity

I need to pass the string curDate between NewDateFragment and NewEventFrament, i see many peoples using Bundle, but using these i still having NullPointerException. I transform the CalendarView to one string named curDate. public class…
1
vote
2 answers

Put long value into Bundle

A Bundle can save many types of data: short, byte, another Bundle, ArrayList, yet not a simple long. How can this best be achieved? Must it be converted to a ArrayList?
serv-inc
  • 35,772
  • 9
  • 166
  • 188
1
vote
1 answer

Storing an ArrayList in an Bundle for use by a Fragment
My application has a view pager including three fragments. These are managed by a FragmentPagerAdapter. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …
ritch
  • 1,760
  • 14
  • 37
  • 65
1
vote
2 answers

Can't get bundle argument in fragment from activity

I am very new as android developer(currently PHP programmer).I am trying learn android by following various tutorials from google developer and other websites. My problem is that i can't get bundle arguments in my fragment which was set in my…
1
vote
1 answer

Parse Bundle data

I'm using GCM in my app and I'm sending some datas from server to client. The data is this: { registration_id: '', time_to_live: 60, 'data.type': 'newProcess', 'data.sessionId': 'fd1eceb1-e065-4d9e-9bda-50aecaf89b68', 'data.questions': […
Lazy
  • 1,807
  • 4
  • 29
  • 49
1
vote
1 answer

The correct way and place to restore bundle information on a fragment?

I have seen Bundles restored in several of the Android callback methods, but in many cases there is manual creation and setting of Bundles as on the developers website, in this case from an external message on Fragment creation: public static…
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
1
vote
1 answer

How to pass ArrayAdapter from Activity to Fragment

I have an app with 1 Activity and 3 Fragments. In the Activity I have an adapter, into which I store log messages - MainActivity.java (keeps the adapter with strings): private ArrayAdapter mLogListAdapter; public void onCreate(Bundle…
1
vote
1 answer

Why intent.getExtras().mMap is null when I debug?

I have a BroadCastReceiver like this: public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println(Arrays.toString(intent.getExtras().keySet().toArray())); …
AngryYogurt
  • 755
  • 2
  • 7
  • 22