Tuesday, 14 March 2017

Pass LinkedHashMap between activities


                   Here I am going to explain how to pass LikedHashMap data between activity.Before that we need to know GSON.Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa.

Add dependencies to gradle

  compile 'com.google.code.gson:gson:2.6.2'


First Activity

        Gson gson = new Gson();

        LinkedHashMap<String, String> map = new 
                                              LinkedHashMap<String, String>();
        map .put("Name", "Dhina");
        map .put("Blog", "Androiddhina");
        map .put("City", "Chennai");
        
        Intent intent = new Intent(activity, DetailsActivity.class);
        intent.putExtra("values",  map);
        startActivity(intent);


Second Activity

         LinkedHashMap<String, String> hashMap = new 
                                     LinkedHashMap<String, String>();

        String str = getIntent().getStringExtra("values");
        Gson gson = new Gson();
        Type entityType = new TypeToken
            <LinkedHashMap<String, String>>() {}.getType();
        hashMap = gson.fromJson(strentityType);


Happy coding :)