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
4
votes
3 answers

How to send RealmObject in Bundle?

How to pass RealmObject through the Intents Bundle? Is there an way to write RealmObject to parcel? I don't want to use Serializable for know reasons.
Dawid Hyży
  • 3,581
  • 5
  • 26
  • 41
4
votes
0 answers

how to get an URI from a Bundle?

I got an Intent that takes a photo, after taking the photo I get the bundle with this.imagen = imageReturnedIntent; this.lol = imagen.getExtras(); I got my "extra" on the Bundle, but I need it in a Uri because after getting the "data" I send it to…
luthor
  • 95
  • 1
  • 5
3
votes
3 answers

Process 'command 'npx.cmd'' finished with non-zero exit value 1 React native build problem

I am working on react native. When i try to create a build of android using gradlew assembleRelease then getting an * What went wrong: Execution failed for task ':app:bundleReleaseJsAndAssets'. > Process 'command 'npx.cmd'' finished with non-zero…
Rover
  • 661
  • 2
  • 18
  • 39
3
votes
0 answers

Using fragment arguments for the entire activity lifecycle

The best practice to instantiate a fragment is: public static MyFragment newInstance(int someInt) { MyFragment myFragment = new MyFragment(); Bundle args = new Bundle(); args.putInt("someInt", someInt); …
3
votes
2 answers

arguments in Fragment is still null

I'm trying to pass two strings through a bundle to my other fragment. Problem is, that arguments are still null in onCreate. Here is my code: class VenueDetailFragment : Fragment() { private lateinit var venueId: String private lateinit var…
Carlo Matulessy
  • 975
  • 1
  • 13
  • 27
3
votes
0 answers

Parcelable with inheritance in Kotlin- Base class properties not getting parcelized

I have a parent class in Kotlin like this open class Parent( var name: String, var dose: JsonElement?, val other: String?) { constructor(name: String, dose: JsonElement?) : this( name = name.toLowerCase(), dose = dose, other =…
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
3
votes
1 answer

Retrieve android.intent.extra.EMAIL value from bundle

I created one app like email clients app such as Gmail. When user click on email address in another apps and choose my app from email sending apps in list appear on . The email content like email address , email subject and .... come to my app by…
SAYE
  • 1,247
  • 2
  • 20
  • 47
3
votes
0 answers

Error on startActivity, but only on android 7 devices

Since I updated my phone (Samsung Galaxy S7) to the new Android 7.0 two days ago I'm getting sporadically an error when running my app. It occurs sometimes (average every fifth time) when starting the Activity_C with startActivity:…
3
votes
4 answers

Activity launched from a service loses the "extra" from the bundle

Calling code (run in service): Intent textIntent = new Intent(this, TextActivity.class); textIntent.putExtra("text_seq", message.xfer.seq); textIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(textIntent); Called code (in…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
3
votes
1 answer

RuntimeException: Parcel android.os.Parcel: Unmarshalling unknown type code when using android bundle

I am getting the below error message: java.lang.RuntimeException: Parcel android.os.Parcel@41141190: Unmarshalling unknown type code 7602286 at offset 16 at android.os.Parcel.readValue(Parcel.java:1921) at…
NewOne
  • 401
  • 5
  • 19
3
votes
2 answers

How to get the getArguments data from a fragment?

I am using a fragment for my application.and I also add a bundle into my fragments but when I want to get the added String from the bundle the compiler shows me a null pointer error like as follows Process:…
3
votes
1 answer

Android cannot get boolean value from bundle

In my GcmListenerService I am getting this bundle data: Bundle[{gcm.notification.e=1, gcm.notification.title=SomeApp, proceed=true, gcm.notification.body=Some text, message=Some message, collapse_key=example.com.SomeApp}] I am can get the message…
3
votes
1 answer

Android L inserts unwanted Intent Extras

My app has been chugging along happily on Android Kitkat and below. However I started getting complaints from Android L users that it crashed on their device. A surprising thing happened while debugging my app on Lollipop. While the debugger was…
2
votes
1 answer

How can I test an older version of my app in Google Play Store?

I'm using Google Play Store to distribute an app. For a certain issue, I would like to install an OLDER version of my app to one of the client's device. I'm struggling to do so, is there any way to do that? No matter how and which "testing" method I…
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
2
votes
1 answer

What is "androidx.lifecycle.BundlableSavedStateRegistry.key" in this bundle?

I'm using toolargetool to investigate why the app is crashing from TransactionTooLargeException. I'm seeing there's this key androidx.lifecycle.BundlableSavedStateRegistry.key which can be around 400 KB when I put the app into the background. What…