Modify data from a different Activity in Android - android

I've been just wondering if there is a way to modify variables from a different activity from which they were created. Precisely, I would like to modify a list in Activity1 from Activity 2, is there a way to give a reference to that list from the other activity? putExtra() method does not accept List as input parameter and I don't think startActivityForResponse() is what I'm looking for either. I don't know if some kind of shared variables exist or something alike.
Is it possible to do that?
Thanks in advance.

May be this is not the very good solution but what you can do is that you can create DataManager as single Instanse that Hold Linklist of data. In each activity you can get instance of data-manager and update data in it.

You can pass the entire list as an extra, you just need to serialize it by making it a parcelable first.
A Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC
A bit confusing to understand, but all it really does is flatten the data into strings/ints/other primitive types so that it can be passed easily. On the other side, it's re-build into your list structure.
This is the tutorial I used when I did something similar:
http://prasanta-paul.blogspot.ca/2010/06/android-parcelable-example.html

Related

When to make an Object parcelable rather than just sending the primitive variables?

After researching for a while there is still something unclear for me:
When is it worth to implement Parcelable rather than just getting the variables and send them to a new Activity?
If i search for example for something like "RecyclerView click open new Activity", almost everyone posts code that extracts the values with getter methods in Activity 1 and sends them via multiple .putExtra calls to Activity 2. Almost no one seems to suggest to implement Parcelable.
I also fail to see where Parcelable saves effort or makes the code more maintainable, since i have to call .getXXX for every value in the target Activity anyways.
So let's say I have a RecyclerView, I want to open a new Activity on Button click and i want to send 3 variables out of 1 Object from 1 ArrayList. Should i send the variables directly over 3 .putExtra calls or implement Parcelable and send the whole Object?
In your case sending the 3 primitive values with putExtra should be enough. Parcelable is to send objects. Normally these objects contain objects which contain other objects or array of objects. In summary it is used to send complex objects. It's also necessary if you need to send an object to your service as part of the body of a request.
Don't use Parcelable if u have less no. Of data elements because serialization itself take some time. But sending them individually will be faster.
Using parcelable for something as trivial as this would be an overkill as it involves the overhead of constructing a new object for only a few values. It would be better if you just add the .putExtra() calls for the values you need to share, that should be enough, as the others before me said. :)

How to exchange an object between different Android Activities?

My application has a list of clients (with only name and age displayed) and I want to be able to edit/add more info about them that is not visible in the list.
So whenever I click on a client, I want to start a second activity with all the info about him.
Can I use an intent for this? Can I pass a full Object (Client) at once with an intent?
I've looked through these two topics, but I haven't found my answer yet:
How to exchange data (objects) between different Android Activities?
How do I pass data between Activities in Android application?
Thanks in advance.
Look at this answer: How to pass an object from one activity to another on Android
//to pass :
intent.putExtra("MyClass", obj);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
I would suggest you look into saving these in the SharedPreferences file. Build a singleton AppUtil class and add functionality to save data in the shared preferences there as well as being able to retrieve said data
If you have a large amount of information being stored for the clients then you should look into SQLite as database storage.
The more pratice way is create a class to hold every objects that you need to change between the activities. Like that:
public class MyHolderObjects {
public static MyObjectType mObject;
}
before start the new activity (or whatever you goes to use it), instantiate (create) the object in MyHolderObjects. And use it everywhere you need :). I prefer this approach instead serialize the object.

"global" array with different data types - Android

I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.

how to combine db4o and android intents

I'm making (my first) android application and I'm a little bit puzzled with the use of db4o.
I have one activity in which I have a listView, and let the user select an object. Then I pass this object trough a series of intents to other activities (to populate its fields) and then back to the main activity.
But the problem is, when I pass an object (it is serializable), the object I get out of the intent is not the same as the one I put in. (different id, when I check with debug).
All the fields are the same, but it's just not 'the same' object.
So when I try to store the updated object in the db4o, it doesn't recognize it, and stores a double.
I've figured out two workarounds:
Also pass an 'original/unmodified' object, and use it to get the db4o reference (through QBE), and then updating the fields of that object with the values of the changed object.
Using global variables so I don't have to use intents (to pass the object)
But both seem really bad to me? What could be a real solution, instead of a workaround?
You could try using a singleton to store your object and the fields that other classes (?) need to set so everyone has access. I'm not clear of your use of intents in this explanation.

Passing array of coordinate[] to a new activity?

I am trying to pass an array of type Coordinates (coordinate[] arrCoordinates) to another Activity class. I find it easy to pass Strings and Ints (e.g intent.putExtra("string", myString); but I can't seem to pass an array of coordinates.
I also tried looking into using Parcelable..but I'm not quite sure on coding it. Could anyone help me out?
In these situations, I have always used the Application class to store global variables.
See this answer for more information.
A dirty hack (which I do not recommend) is to make your array a public static member of your first Activity class in order to easily grab a reference into your new Activity.

Categories

Resources