Android:Passing a hash map between Activities

后端 未结 5 1032
我在风中等你
我在风中等你 2020-11-27 03:59

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         


        
5条回答
  •  萌比男神i
    2020-11-27 04:45

    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.

提交回复
热议问题