I have a ListView that look at a list of my protobuf object. When the user click on an item in the ListView, I want to show the details of that object. Currently, I am implementing this as a separate activity. Before I start the activity, I need to pass in the object data.
Should I serialize my protobuf object and pass it in the child activity? The child activity will have to deserialize the protobuf object.
Or should I provide some kind of accessor for my child activity to modify private fields of the parent activity?
Should I serialize my protobuf object and pass it in the child activity? The child activity will have to deserialize the protobuf object.
Yeah this is the way to go, :) ... or you can use Parcelable. Another option is to extend the Application class and have it store the protobuf data. This data will be accessible in all of your Activities.
Or should I provide some kind of accessor for my child activity to modify private fields of the parent activity?
No this is a bad idea
Related
So let's say I have this list activity and when you click an item on the list it takes you to a new activity with more details on that list item. All this info is fetched using REST services. Before I'd use Retrofit to store that data into an object and store that into and singleton class that is accessible by both activities so that when one list item is selected it just takes the id and goes to the singleton to get the right data then use that in the new activity. Is this proper Android practice? Also I was wondering if it's better to have the list activity go to a new activity to present more detail info or use a fragment.
It's your choice to take the details screen as Activity or Fragment.
Fragments are better.
And you can pass the list by making the pojo class as Parcelable between activities or fragments.
Yes, usually we can use Static variables or singleton classes to transfer parameters between activities. You can also use intents to pass variables from one activity to another.
What is the best solution to deal with an "AdapterA of a RecyclerViewA which is inside a FragmentA" that want to use data from another "AdapterB of a RecyclerViewB which is inside a FragmentB" ?
I am stuck,i tried to :
1- Make the data static in the adapter (no garbage collector)
2- Duplicate data that i need (waste of memory)
(it works but may be there is a better solution)
Thank you in advance. (plz ask for details if you need)
If you, in MainActivity, create a new instance of a class where you contain whatever you want both fragments to access. If you implement Serializable or Parcelable, you can also send it to each fragment using Bundle/Intent.
Then, as you have the same instance in two different fragments, if you edit data in fragment X, fragment Y will be able to access it.
See this:
|---MainActivity---|
| | |
V V V
Frag A <->Data <->Frag B
MainActivity creates a new class(data) which it sends to each of the fragments. The fragments can update the data in the class. Please note, that you have to use class if any given data type isn't supported.
If you don't feel like using Serializable or parcelable, if possible, send MainActivity as an instance to each of the fragments. From each fragment you then get the MainActivity instance and find the data you need.
If you can't pass MainActivity to either of the fragments, and cannot use Serializable/Parcelable and the data type isn't supported by bundle.putExtra or intent.putExtra, you have to use a static import.
Those are your only options.
Alternatively, you can create a class that extends "Application". Then you write:
MyApplicationClass mac = (MyApplicationClass) getApplicationContext();
Then you access the data in the application-extending class(here: the mac instance)
Final words
If you do not want to use static instance, send a parcelable/serializable class with the contents, or use a class extending Application, there is no way you can transfer the data(considering you are using a HashMap which you claim cannot be sent by Intent or Bundle). If you had a data type or class that was possible to send using Intent or Bundle, you wouldn't have to use static instance or parcelable/serializable class. But with the position you are in, I have presented all the options you have. There are basically no other ways than using a class containing the hashmap, using a static instance or utilizing the Application class.
I use the putExtra and getSerializable Methods to pass my object to a second activity. It works fine, however, am I required to return this object in order to maintain the changes made in the second activity?
When I run my app, and launch my second activity then call finish() after makimg a change to the object passed to it, if I relaunch that second activity the old object data previous to the change is displayed, does this mean that using the put/get serializable methods are passing a clone of the object, and that in order to keep the changes made on the second activity I must repass the object back to the main activity ?!
I am not sure why would you require such behaviour.However you can try the following methods.
You can make that object as global static variable(preferably in the application class of the app) so that the object is retained between different instances of the activities.
Also if the state of the object is important across app restarts you must plan to write the state of the object in some persistent storage like db/file/shared preference.Refer this link for storing object,
I want to pass an ArrayList of custom Objects to a FragmentActivity. Each Fragment will have information populated by an individual Object.
My question is, when I use the set methods (in the custom Object) in each Fragment, will that correctly update my ArrayList of these custom Objects so that I can pass this updated ArrayList of Objects to another Activity?
Yes, as long as the reference to the object is the same the changes are made.
In my main Activity I have object with some data in it. I want to delete all this data through PreferenceActivity using method (wipe()) that does this job. How can I do it?
Inside Preferences.java there is a OnPreferenceClickListener which, when activated, expected to use method in object that I need to access.
Thanks in advance.
Just make this object as static.
Then you can access to this object by class name.
Activity.counters.wipe();
But, keeping data in activities is a bad practice in android development