I'd like to pass an entire (custom) class reference to another activity.
This class is called WeekProgramData. WeekProgramData has an array of 7 Day (class) instances and each Day class has several Switch instances.
How can I pass along the class reference between activities, so I can use the methods from that class etc. in other activities?
I already tried the following, but it failed: First, declaration in Activity A
WeekProgramData wpd = new WeekProgramData();
Code for passing the WeekProgramData class reference in activity A:
Intent intent = new Intent(v.getContext(), WeekOverview.class);
intent.putExtra("wpd", wpd);
startActivity(intent);
Code in Activity B for getting the class reference:
Bundle extras = getIntent().getExtras();
WeekProgramData wpd = extras.getWeekProgramData("wpd");
To pass a class via intent you have to implement the Parcelable interface. Here is a tutorial on how to do it and it shouldn't take you too long. Once you do this you can pass instances of that class via an intent to whatever you want.
The way to pass custom classes between activities is to make your classes Parcelable. This is a way of serializing your data so it can easily be sent around. Once your classes are Parcelable you can use the standard putExtra and pass your class in. To retrieve your class you can use getParcelableExtra. With Parcelable remember you cannot parcel null values and all nested classes must be parcelable as well. In your case you will have to make your WeekProgramData class parcelable as well as your Day and Switch classes.
There could a couple of diiferent ways to do this, but the two best options in my opinion are:
WeekProgramData should implement Parcelable
or convert wpd into String using Gson, pass that string to your second activity and finally reconstruct your object from that string using Gson again
Related
My app contains 2 activitys. Activity A is the one which is created by starting the app. In this one I create an object of my own class MyClass. This class contains one string and 3 integers. In activity A this object gets written.
The second activity B needs to read this object. How can I pass it from A to B? Or is there an other solution?
There are couple of way you can pass an object from one activity to another:
1. Application Class: this class is visible to all your application Activities so you can save your object in this class from one Activity and then access it from the other.
2. You can break apart your Class into the simple variables: string and 3 integers and pass them via a bundle or the intent it self from one activity to another, then construct your object again.
Intent intent = new Intent (this, TargetActivity.class);
intent.putExtra(KEY, value);
intent.putExtra(KEY, "value");
startActivity(intent);
3. If your object implements Serializable/Parcelable then you can pass it via a bundle.
Example on how to serialize an object:
How do I serialize an object and save it to a file in Android?
One option could be implementing Serializable interface and then you can pass object instances in intent extra using putExtra(Serializable..) variant of the Intent#putExtra() method.
//to pass :
intent.putExtra("MyClass", obj);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
It can be tricky, because there's no guarantee that your application can't be killed between activities. Actually, it can be killed during activities, so keeping persistent objects around can be tricky.
My preferred way to do this is the "singleton pattern" in which you create a class whose purpose is to create a single instance that holds whatever data you want to hang around. If your application gets killed, the singleton instance will be lost and have to be re-created, but all Android apps run this risk all the time anyway.
See Save multiple instances states of the same Activity in Android for my implementation of a singleton in Android.
Oh, and I should add that this only works within an application where all the activities are in the same process, sharing the same address space. Otherwise, you'll have to make your object serialiazable and write it off to a file.
There are two activity classes in my project and a third class which is subclass of Thread.
Thread Class implements Bluetooth Socket which isn't Parcelable.
First Activity starts the Second Activity using startActivityforResult()
Second Activity creates an object of the Thread class and starts the thread.
I need to pass an object reference of the Thread object from Second Activity to First Activity's onActivityResult() so that I can access Thread object from the first activity.
How can I achieve this?
You have a few options.
You can either break down your object into simple data types and put those values as extras on the intent that you pass back with setResult(), do do so you'd use intent.putExtra(key, value)
Or you can make your data object implement the Parcelable interface so that you can add the data object directly to the intent.
the code to do the latter would look something like this
Intent resultIntent = new Intent();
resultIntent.putExtra("resultObject", mObj);
setResult(ACTION_OK, resultIntent);
then inside your onActivityResult you can pull it out like this:
data.getParcelableExtra("resultObject");
For the latter method to work you need to correctly implement parcelable with your data object. The former method does not require this however, since you'll be passing back simple values only. You'd then have to take those simple values and "re-inflate" the data object on the other side.
I think the best way to achieve this would be to use a singleton. You can only store primitives in Shared Preferences and Bundles. Here is a great reference for creating a singleton.
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
I am implementing the DataDroid model of RESTful communication and I have come across a compiler error while implementing my SearchCriteria. The problem is that in order to pass the SearchCriteria around as an intent extra, I had to make it implement Parcelable. However, my Worker's start function requires a SearchCriteria class, leading to the error: Required: my.classes.SearchCriteria; Found: android.os.Parcelable.
Assuming that I've correctly implemented Parcelable for my SearchCriteria class, how can I quickly create an object from a parcel (where the parcel is found using getParcelable Extra(INTENT_NAME)?
Edit: I realize I can accomplish this quickly by making my constructor for SearchCriteria from Parcel public, but is there another way? Actually, this does not work - I confused a Parcel with a Parcelable thing.
Suppose you follow the API and make SearchCriteria implements Parcelable properly, and your SearchCriteria has been properly constructed or instantiated from the underlying business layer, either from a Database or a Http server or etc.
To pass it to next activity by intent:
SearchCriteria searchCriteria = createSearchCriteria();
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("searchCriteria", searchCriteria);
startActivity(intent);
To retrieve it from intent in next activity:
SearchCriteria searchCriteria = getIntent().getParcelableExtra("searchCriteria");
myWorker.search(searchCriteria);
In most situation, we don't need bother Parcel directly.
I have a class 'Product' and i need to pass the arraylist of 'Product' objects from one activity to another.
I read that i can do so by making my Product class as Parcelable and use : putParcelableArrayListExtra (String name, ArrayList value).
In my app, i need to pass arraylists of objects from one activity to another. In this case, i need to make all those classes as parcelable and implement the interface methods.
Is this the correct way to solve the problem? Is there any other way to send the list of objects in intents?
Thanks..
Yes, the correct way is to use Parcelable or do the serialization to another kind of primitive object that you can put to Intent's extras
I have an object that has (among other things) a list of Intents. I want to pass this object as an extra to an Intent. However, the Intent class is not serializable, it is just "Parcelable".
I assume that Parcelable is the android version of Serializable, but I'd rather not have to write my own serialization code for my class, and Parcelable seems to require that.
Anyone have any solutions to this other than just reimplementing the Intent as a serializable class?
You can put a Parcelable in an Intent extra, and an Intent is already Parcelable. All you need to do is make your object Parcelable and you are set.
Another Route you can take:
make your list of intents transient(not included in a serializable),
then pass it as a parcelable array via Intent.putExtra(String key, Parcelable[] value).
The receiving class can then recreate your Object.