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

Send data from Activity to a Fragment using Bundle without committing Fragment transaction

I know that data can be transferred from an Activity to another Fragment using Bundle. I have done some research and I found out that Bundle works only when the Activity commits a Fragment transaction to the Fragment where the data has to be…
Tony Mathew
  • 181
  • 1
  • 9
-2
votes
1 answer

Getting null values when passing data from activity to fragment using Parceable

I am trying to pass arraylist from activity to fragments but i am getting a null value using the parceable. In Main Activity: Bundle bundle = new Bundle(); bundle.putParcelableArrayList("cbookings", customerbooking); …
AXIES
  • 13
  • 5
-2
votes
1 answer

Pass the custom list value adapter to activity

I am using Retrofit concept, I need to pass the list value to activity. Here I attached the code where I get the response from backend. private void callCheckTopCourseDetails(String getId) { APIInterface apiInterface =…
-2
votes
1 answer

Android app crashes when passing through a bundle through intent

I am kind of new to Android Development. In my activity, i am trying to pass through some strings using bundle and intent to the next activity. But for some reason the app crashes and throws an error: FATAL EXCEPTION: main The logcat when the…
-2
votes
2 answers

Fragment reload causes data lost

I have an activity with 2 fragments used as tabs.I have some data that i pass to the fragments via bundle. The problem is that my fragments reload and the bundle turns into null which causes null pointer exception when i try to get the data. I tried…
-2
votes
5 answers

How setcontentview in a new activity depending on the button clicked

I've been searching the forum but can't seem to find what I'm looking for... I've got a xml with some 40 buttons. Whenever a specific button is clicked I want to open a new activity and set the contentview depending on the button clicked. Opening…
-3
votes
2 answers

i have an error in my rootView

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { et=(EditText)rootView.findViewById(R.id.et1); String str=et.getText().toString(); Fragment…
user3409897
  • 143
  • 1
  • 2
  • 8
-3
votes
1 answer

Android how to get/retrieve the data from bundle?

I'm sending data from one class using this intent.putExtra("alarmMgr_id", alarm_id); I'm getting the data in the other class in the bundle but I don't know how to retrieve it from the bundle im attaching a screenshot: Please guide me how to get…
mushahid
  • 456
  • 5
  • 20
-4
votes
1 answer

Not able to get serialized bundle in an activity

I have a class public class Alarm implements Serializable. This has a method in it: public void schedule(Context context) { setAlarmActive(true); Intent myIntent = new Intent(context, AlarmAlertBroadcastReciever.class); Bundle bundle =…
Umar Ali
  • 13
  • 6
-4
votes
2 answers

Passing data from an Activity to a fragment that has no onCreate

Is it possible to pass data from an Activity using Bundle to a Fragment that has no OnCreate
-4
votes
4 answers

how to send all the data of array list to another activity?

Daily Screen :- public class DailyScreen extends AppCompatActivity implements AdapterView.OnItemClickListener { ImageView add, edit; MyCustomAdapter adapter1; Button ok; Button next2; final Context context = this; ListView…
-7
votes
5 answers

How can I transfer some value between activities in Android

I tried to send a int value from current activity to the new one, here is the parts in current activity. dialog.setPositiveButton("4 players", new OnClickListener() { @Override public void…
SonicFancy
  • 241
  • 1
  • 2
  • 10
1 2 3
18
19