I need to pass a reference to the class that does the majority of my processing through a bundle.
The problem is it has nothing to do with intents or contexts and ha
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);
another simple way to pass object using a bundle:
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
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.
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.