Android:Passing a Hash Map Between Activities

Android - How to pass HashMapString,String between activities?

This is pretty simple, All Collections objects implement Serializable (sp?) interface which means they can be passed as Extras inside Intent

Use putExtra(String key, Serializable obj) to insert the HashMap and on the other Activity use getIntent().getSerializableExtra(String key), You will need to Cast the return value as a HashMap though.

Android:Passing a hash map between Activities

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.

passing a HashMap from one activity to another, android

Use intent.putExtra(String, Serializable) - see http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29.
i.e.

intent.putExtra("placesHashMap", places)

In the receiving activity use

HashMap<String, String> places = (HashMap<String, String>) intent.getSerializableExtra("placesHashMap");

How to send hashmap value to another activity using an intent

Java's HashMap class extends the Serializable interface, which makes it easy to add it to an intent, using the Intent.putExtra(String, Serializable) method.

In the activity/service/broadcast receiver that receives the intent, you then call
Intent.getSerializableExtra(String) with the name that you used with putExtra.

For example, when sending the intent:

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

And then in the receiving Activity:

protected void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
Log.v("HashMapTest", hashMap.get("key"));
}

pass hashmap data from an activity

Use intent.putExtra(String, Serializable) - see

intent.putExtra("hashMap", fileList);

In the receiving activity use

 HashMap<Integer, boolean[]> fileList = (Integer, boolean[]) intent.getSerializableExtra("hashMap");

And also chnage the below code

Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);

To

Intent intent = new Intent(context, MainActivity.class);

passing hash map to an intent

use this

   intent.putExtra("hash",data.toString());

OR

   intent.putSerializableExtra("hash",data)

Passing Hashmap through android intents

Sending HashMap

HashMap<String, Object> hashmapobject =new HashMap<>();
HashMap<String, Object> newhashMap=new HashMap<>();
hashmapobject.put("key",newhashMap);

Intent intent = new Intent(SenderActivity.this, NextActivity.class);
intent.putExtra("hashMapKey", hashmapobject);
startActivity(intent);

Receiving HashMap

Intent intent = getIntent();    
HashMap<String, Object> hashMapObject = (HashMap<String, Object>) intent.getSerializableExtra("hashMapKey");
HashMap<String, Object> newhashMap=(HashMap<String, Object>)hashMapObject.get("key");

Pass data between two activities in android using HashMap

Using WeakReference<Object> will clear the data variable in DataHolder class when code reach to setContentView(R.layout.Some_Activity) line in second activity. So I use Object instead of WeakReference<Object> .

DataHolder.java

public final class DataHolder {
private static Map<String, Object> data = new HashMap<String, Object>();

public static void setF_TaskId(int F_TaskId) {
data.put("F_TaskId", F_TaskId);
}

public static int getF_TaskId() {
Object resault = data.get("F_TaskId");
return (Integer)resault;
}
}


Related Topics



Leave a reply



Submit