Android Intent Passing custom object between activitiesIt is also possible to pass your custom object to other activities using the Bundle class.
You can pass values from one activity to another activity using the Bundle. In your current activity, create a bundle and set the bundle for the particular value and pass that bundle to the intent. Intent intent = new Intent(this,NewActivity. class); Bundle bundle = new Bundle(); bundle.
You can also use Gson to convert an object to a JSONObject and pass it on bundle. For me was the most elegant way I found to do this. I haven't tested how it affects performance.
In Initial Activity
Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myObject", new Gson().toJson(myobject));
startActivity(activity);
In Next Activity
String jsonMyObject;
Bundle extras = getIntent().getExtras();
if (extras != null) {
jsonMyObject = extras.getString("myObject");
}
MyObject myObject = new Gson().fromJson(jsonMyObject, MyObject.class);
Figuring out what path to take requires answering not only CommonsWare's key question of "why" but also the question of "to what?" are you passing it.
The reality is that the only thing that can go through bundles is plain data - everything else is based on interpretations of what that data means or points to. You can't literally pass an object, but what you can do is one of three things:
1) You can break the object down to its constitute data, and if what's on the other end has knowledge of the same sort of object, it can assemble a clone from the serialized data. That's how most of the common types pass through bundles.
2) You can pass an opaque handle. If you are passing it within the same context (though one might ask why bother) that will be a handle you can invoke or dereference. But if you pass it through Binder to a different context it's literal value will be an arbitrary number (in fact, these arbitrary numbers count sequentially from startup). You can't do anything but keep track of it, until you pass it back to the original context which will cause Binder to transform it back into the original handle, making it useful again.
3) You can pass a magic handle, such as a file descriptor or reference to certain os/platform objects, and if you set the right flags Binder will create a clone pointing to the same resource for the recipient, which can actually be used on the other end. But this only works for a very few types of objects.
Most likely, you are either passing your class just so the other end can keep track of it and give it back to you later, or you are passing it to a context where a clone can be created from serialized constituent data... or else you are trying to do something that just isn't going to work and you need to rethink the whole approach.
The Parcelable interface is a good way to pass an object with an Intent.
How can I make my custom objects Parcelable? is a pretty good answer on how to use Parcelable
The official google docs also include an example
You could use the global application state.
Update:
Customize and then add this to your AndroidManifest.xml :
<application android:label="@string/app_name" android:debuggable="true" android:name=".CustomApplication"
And then have a class in your project like this :
package com.example;
import android.app.Application;
public class CustomApplication extends Application {
public int someVariable = -1;
}
And because "It can be accessed via getApplication() from any Activity or Service", you use it like this:
CustomApplication application = (CustomApplication)getApplication();
application.someVariable = 123;
Hope that helps.
You can also make your objects Serializable and use the Bundle's getSerializable and putSerializable methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With