Android:Passing a hash map between Activities - android

I have a map between string and a class object. I populate this map in Activity1 and i would like to pass it to activity2.
public class NEW extends Activity {
public class data {
String name;
float value;
.... etc }
......
static Map<String, data> data_map = new HashMap<String, data>();
..... }

The best way to do this is if you can express your data in the primitives supported by Bundle, so it can be placed in the Intent you are sending through the Intent.putExtra() methods. (EXCEPT for the use of Serializable, which is extremely slow and inefficient.)
However you can't do this because (a) you are using a Map and (b) your map contains a custom data type.
The formally correct solution to this exact problem is to write a custom Parcellable class that takes care of marshalling/unmarshalling your data structure. I'll sketch out the code here, though it may not be exactly correct:
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
public class MyData implements Parcelable {
HashMap<String, data> data_map = new HashMap<String, data>();
public MyData() {
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int parcelableFlags) {
final int N = data_map.size();
dest.writeInt(N);
if (N > 0) {
for (Map.Entry<String, data> entry : data_map.entrySet()) {
dest.writeString(entry.getKey());
data dat = entry.getValue();
dest.writeString(dat.name);
dest.writeFloat(dat.value);
// etc...
}
}
}
public static final Creator<MyData> CREATOR = new Creator<MyData>() {
public MyData createFromParcel(Parcel source) {
return new MyData(source);
}
public MyData[] newArray(int size) {
return new MyData[size];
}
};
private MyData(Parcel source) {
final int N = source.readInt();
for (int i=0; i<N; i++) {
String key = source.readString();
data dat = new data();
dat.name = source.readString();
dat.value = source.readFloat();
// etc...
data_map.put(key, dat);
}
}
}
Note that when you have a custom data structure like your "data" class, it can be cleaner to also make that Parcellable, so it knows how to read/write its contents in a Parcel, and the code here would just call .writeToParcel(...) and a .readFromParcel(...) method on it instead of knowing the details of its contents. That way when you add new fields to "data" you don't forget to also update this other marshalling code to know about them.

I am assuming you own both Activities (call them A and B). In which case just put the map in a public static variable and access it from B via A.data_map.
[update]
For all of the downvotes take a peek at the Android Application Framework FAQ section "How do I pass data between Activities/Services within a single application?". The solution I recommend is exactly the same as...
A public static field/method
An alternate way to make data
accessible across Activities/Services
is to use public static fields and/or
methods. You can access these static
fields from any other class in your
application. To share an object, the
activity which creates your object
sets a static field to point to this
object and any other activity that
wants to use this object just accesses
this static field.
Yes, there are caveats to this solution but with the limited info presented by the OP we can not assume this method will not work.

I would suggest using Intents, which work for both static and arbitary objects (as long as they implement Serializable). Create a custom Intent for your application and then pass on your HashMap (not a Map, which doesn't implement Serializable!) as extra data:
Intent act2 = new Intent(Activity2.SHOW_ME);
act2.putExtra("data", data_map);
Then in Activity2, you can call getIntent() and check via Intent.getAction().equals(Activity2.SHOW_ME) whether you were the one calling your Activity. If so, you can access your extra data by
Intent caller = getIntent();
if (caller.getAction().equals(Activity2.SHOW_ME)) {
Map<String, NEW.data> data_map = (Map<String, NEW.data>)caller.getExtras().get("data");
}
Hope I typed everything correctly ;) This assumes, that your Intent action-string is stored as static final string SHOW_ME = "yourpackage.yourname"; in Activity2.
If you need further clarification, add a comment.

You can encapsulate your data in a Bound Service as described in this document:
http://developer.android.com/guide/topics/fundamentals/bound-services.html
Don't forget to add your server to the manifest file.
this solution has the advantage of decoupling your data from your UI, which will ultimately lead to a more maintainable design.
If the need arrises you can add a messaging wrapper to your service's API to allow it to be called from other processes / applications

Create a new java file which will be global for whole application.
Step1:
public class GlobalClass extends android.app.Application
{
public static Map<String, NEW.data> data_map = new Map<String, NEW.data>();
}
Step2:
After doing that, Register this "GlobalClass" in AndroidManifest.xml
<application
android:name=".GlobalClass"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
</application>
Step3:
now you can use this Map anywhere in your application.
#Override
protected void onCreate(Bundle icicle)
{
GlobalClass global = (GlobalClass)getApplication();
Map<String, NEW.data> my_map_data = global.data_map;
}
These steps maybe helpful for you...

Related

How to extend and use Application properly

In my application i have to share various java-beans class among the activities.
In order to do that, i extended the Application class, in which i create an HashMap filled with all the java-beans. Each java-beans has its own Key.
Example code:
public class MyApplication extends Application {
public static final String CLASSROOM_KEY = "Classroom";
private HashMap<String, Object> myObjects;
#Override
public void onCreate() {
myObjects = new HashMap<String, Object>();
myObjects.put(CLASSROOM_KEY, new Classroom());
}
public HashMap<String, Object> getMyObjects() {
return myObjects;
}
}
This is usable in all the activities, and this is ok. BUT, i have two problems:
1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.
For example, from my main activity i start a service (but it is in a normal class), and the service calls a query that in turn is in another normal class.
The query needs an object that is in myObjects!
I can't make myObjects public static i think.
2) In MyApplication i have to create all my java-beans in advance.
What if in the future i wanted to create a new classroom object in addition to the already present one?
I should create a new key for it, but it is impossible!
Thanks for your help.
UDPATE
I change the question:
In this class:
public class ClassroomUtils {
private static String result = null;
private static String studentObjectID = null;
public static void queryClassroom(String UUID, final Classroom classroom) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Classroom");
query.whereEqualTo("BeaconUUID", UUID);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
try {
result = object.getString("Label");
} catch(Exception e1){
System.out.println("Vuota");
}
if(result != null) {
Log.i("Classroom", "Retrieved " + result );
classroom.setClassroom(result);
sendNotification(result);
addStudentClassroomRelation(object);
}
} else {
Log.e("Classroom", "Error: " + e.getMessage());
}
}
});
}
i want to avoid to pass the classroom to this method (called from another normal class). How can i access to global objects from this class?
I can't make myObjects public static i think.
Why not? myObjects is effectively global in scope already. There is nothing to be gained, from a memory management standpoint, by having myObjects be a private data member of Application. If you want Classroom to be a Java singleton, do so. You just have to watch your memory management, as you do with your current implementation.
In MyApplication i have to create all my java-beans in advance
No, you do not.
What if in the future i wanted to create a new classroom object in addition to the already present one?
Then create another one. Perhaps the right singleton is a School, which holds onto a collection of Classroom objects. Again, your primary near-term issue is one of memory management, so you do not run out of memory because you are trying to keep these objects around all of the time.
1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.
The best way, I think, is to create the MyApplication class as a singleton. There you can retrieve the data from anywhere by calling getInstance and the corresponding getter/setter for your attributes.
Short example:
public class MyApplication extends Application {
private static MyApplication mInstance;
public MyApplication getInstance(){
// this means you have only one existing instance of this class.
if(mInstance == null){
// set the context to this MyApplication instance
mInstance = this;
}
// return the instance of this class
return mInstance;
}
// here your stuff for MyApplication
public HashMap<String, Object> getMyObjects() {
return myObjects;
}
}
Then you can call it from another class like this:
public class CFoo{
public CFoo(){
//retrieve myObjects from MyApplication
MyApplication.getInstance().getMyObjects();
}
}

How many ways we can pass data (objects) between activities in android?

I hope that we can pass data between android application components
by following ways.
1.we can pass data using intent object,
2.we can implement serializable , parcelable interface and pass objects by using intent,
3.we can create a new class by extending Application class, to access global members from anywhere
the android application,
4.sharedpreference ,
5.sqlite.
Are there any other mechanism to send data between android application components?
Another option is create ApplicationPool.
Follow the below steps:-
Initiate the ApplicationPool :-
ApplicationPool pool = ApplicationPool.getInstance();
modify the data on details page and add to pool
pool.put("key", object);
get the modified data on list page from pool
Object object = (Object) pool.get("key");
important notes:- notify the listview or gridview after getting the data
ApplicationPool class file
public class ApplicationPool {
private static ApplicationPool instance;
private HashMap<String, Object> pool;
private ApplicationPool() {
pool = new HashMap<String, Object>();
}
public static ApplicationPool getInstance() {
if (instance == null) {
instance = new ApplicationPool();
}
return instance;
}
public void clearCollectionPool() {
pool.clear();
}
public void put(String key, Object value) {
pool.put(key, value);
}
public Object get(String key) {
return pool.get(key);
}
public void removeObject(String key) {
if ((pool.get(key)) != null)
pool.remove(key);
}
}
Another way is to use static elements, wether it be:
Static fields (with public access for example)
Static properties (meaning private fields with getter and/or setter)
Singletons
Possibly nested classes
While the use of static variables in OOP is debatable, they introduce global state and therefore are a way to accomplish sharing of data inbetween activities too.
1) HashMap of WeakReferences, for example:
public class DataHolder {
Map<String, WeakReference<Object>> data = new HashMap<String, WeakReference<Object>>();
void save(String id, Object object) {
data.put(id, new WeakReference<Object>(object));
}
Object retrieve(String id) {
WeakReference<Object> objectWeakReference = data.get(id);
return objectWeakReference.get();
}
}
Before launching the activity:
DataHolder.getInstance().save(someId, someObject);
From the launched activity:
DataHolder.getInstance().retrieve(someId);
2) Or strange method: store data on server O_o

Is it possible to send a custom object with an Intent as an Extra

I'm asking this question: instread of giving a string, a int and so on, can we push a custom object during the creation fo a new Intent?
newActivity.PutExtra("JsonDataResult", business.getJSON());
In fact I have one object constructed thanks to a JSON (from webrequest) , I parse it and I put it on an object.
At this point I'm passing the string returned from the webrequest to another intent but the parsing takes a long time tu be done, so it could be super-cool the ability to pass custom object with intent.
EDIT : I'm using monodroid / xamarin, so
Android.OS.IParcelable cannot be implemented,
Java.IO.ISerializable cannot be implemented.
You can either let your custom classes implement Parcelable (Google says its faster, but you have to do more coding) or Serializable.
Then add your objects to a bundle (or to the "extra"):
Bundle b = new Bundle()
b.putParcelable("myObject",myObject);
b.putSerializable("myObject",myObject);
For info to Parcelablecheckout this
And if you're interested in the difference between Parcelable and Serializable in more detail check out this
I personally prefer the usage of Serializable for simple object-passing, since the code ist not spoiled with so much code.
Edit: ok isn't your question very similar to this then?
As you've specified you're using Monodroid, it looks like it's not straightforward. I did a quick search and found this forum post
Which listed the following solutions to this problem in Monodroid:
Store the custom Object to be passed as a global variable somewhere, and just read it from your second activity
Which is a bit messy and bad practice, but would work.
Or
serialize your class to a string and send the string to the second Activity
Which will be a little more hard work, but better practice
This is an example how to create a Parcelable class:
public class Person implements Parcelable {
private String name;
private String surname;
private String email;
// Get and Set methods
#Override
public int describeContents() {
return hashCode();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(surname);
dest.writeString(email);
}
// We reconstruct the object reading from the Parcel data
public Person(Parcel p) {
name = p.readString();
surname = p.readString();
email = p.readString();
}
public Person() {}
// We need to add a Creator
public static final Parcelable.Creator<person> CREATOR = new Parcelable.Creator<person>() {
#Override
public Person createFromParcel(Parcel parcel) {
return new Person(parcel);
}
#Override
public Person[] newArray(int size) {
return new Person[size];
}
};
Give a look here if you want to use Parcelable.

android pass ArrayList<self_Obj> by Intent

I had create a class named ChannelObj that contains values like this
public class ChannelObj {
public String enable;
public String id;
public String name;
public String ptz;
public ChannelObj(Node n){
this.enable = n.getAttributes().getNamedItem("enabled").getNodeValue();
this.id = n.getAttributes().getNamedItem("id").getNodeValue();
this.name = n.getAttributes().getNamedItem("name").getNodeValue();
this.ptz = n.getAttributes().getNamedItem("ptz").getNodeValue();
}
}
and this Class can create Obj that contains what data I need;
after that,I have an ArrayList named allChannel contains all ChannelObj i have
like this
for(int i = 0;i<num_of_channel;i++)
{
allChannel.add(new ChannelObj(n1.item(i)));
}
i've checked the data in allChannel is correct
but i want pass this ArrayList to next Activity
i've tried ways like
Intent i = new Intent(this,ChannelListActivity.class);
Bundle b = new Bundle();
b.putParcelableArrayListExtra("dd", ArrayList<ChannelObj> allChannels);
i.putExtra(String name,b);
startActivity(i);
but didn't work and still wrong
what i suppose to do?
thanks for your help!
An alternative to the answer given by Benoir is to have your ChannelObj class implement the Serializable interface. You're only using simple data types, so all the (de)serializing will be automa(g)(t)ically done underwater.
If your class implements Serializable, then you can add it to a Bundle as follows:
bundle.putSerializable("CHANNELOBJ_LIST", mChannelObjList);
Note that you may need to cast to an ArrayList<ChannelObj> (or some other concrete implementation of List<T>) as the List<T> interface does not implement Serializable.
Retrieving the list of objects in the next activity is similarly easy:
List<ChannelObj> mChannelObjList = (ArrayList<ChannelObj>) bundle.getSerializable("CHANNELOBJ_LIST");
Your clas must implement Parcelable check it out here: http://developer.android.com/reference/android/os/Parcelable.html

Regarding Intents

According to what i have learnt from passing data using Intents is that when you pass Object O from Activity A to Activity B via intents, activity B receives a COPY of object O. The way things work is that The object O gets serialized (converted to a sequence of bytes) and that sequence of bytes is then passed to Activity B. Then activity B recreates a copy of object O at the moment it was serialized.
I would like to know if it would be efficient if one extends the Intent class to create a custom Intent and have references to the objects that are required by the other activities and pass the data to the other activities. For example:
public class CustomIntent extends Intent {
private Object o;
public CustomIntent() {
super();
// TODO Auto-generated constructor stub
}
public Object getObject () {
return o;
}
public void setObject(Object object) {
this.o = object;
}
}
In the receiving activity i get the intent and cast the intent to the CustomIntent type and retrieve the object required by the activity. Would this improve the efficiency by reducing the need for Serialization? Kindly throw some light on this. Thanks in advance.
No. Intents are dispatched by the Android system and are always serialized as they can be sent to any activity, service, etc in the system.
For your problem you could probably workaround this issue by creating an Application class and storing your data in it:
class CustomApplication extends Application {
private Object data;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
You activate it by updating AndroindManifest.xml setting the android:name property on the application tag you your class name.
To use in your activities:
CustomApplication app = (CustomApplication) getApplicationContext();
app.setData(yourDataObject);
I think it would be better if you let the android handle everything for you. Do not customize it, if it is not very essential.
If you want to have the reference of the object in another activity then there are other ways too.
You can make your object static and directly access it from other activity.
You can make a new object of same type and replace it after coming again back to the first activity(in onActivitResult() method.).
or there may be many more ways to do it.
Thanks.

Categories

Resources