0

I have used this tutorial to save session. I still don't understand how to get this data from another activity.

 public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
      user.put(KEY_FULLNAME, pref.getString(KEY_FULLNAME, null));        
      user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

    // return user
    return user;
}

I found out people are using intent to send it to another activity like in this link. Is there a simple way to get the data like i call methods from another activity(import the class, then give a variable var for that class, and call the method like var.theMethodFromClass()).


Update

public SessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

this function is in my SessionManager class, how can i use it directly, without initializing all variables again, as they are already initialized in the activity in order to be used.

the full code of SessionManager.class is in the first link

Community
  • 1
  • 1
Filip Luchianenco
  • 6,912
  • 9
  • 41
  • 63

5 Answers5

1

You are already saved your data in the SharedPrefernces so why you need to send it from one Activity to another Activity..Try to open the SharedPreference file in Another Activity and Use it as you like..

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • `SharedPreferences pref; String name = pref.getString(KEY_FULLNAME, null);` like this? but i have to initialize `pref` – Filip Luchianenco Nov 08 '13 at 11:03
  • 1
    yes..and initialize the preference with the same file name as you mentioned in Activity1..you have to use the same key for retrieving the data.. – kalyan pvs Nov 08 '13 at 11:05
1

Save data to preferences in calling activity.

public void saveToPreferences(HashMap<String, String> user)
{
SharedPreferences pref = getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(KEY_FULLNAME, user.get(KEY_FULLNAME));
editor.putString(KEY_EMAIL, user.get(KEY_EMAIL));
    editor.commit();
}

In your called activity. use this code. Initialize preferences 1st then get data from it.

public HashMap<String, String> getUserDetails(){
SharedPreferences pref = getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_FULLNAME, pref.getString(KEY_FULLNAME, null));        
    user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));


    return user;
    }
Ahmad Raza
  • 2,850
  • 1
  • 21
  • 37
0

If you want to save the HashMap and use it even after the application is restarted, then use SharedPreferences. It will save the HashMap in the device.

If you just need to pass the HashMap as parameter to another Activity, use Intent. All that you pass by Intent's must be serializable to work well in all cases. Read: http://developer.android.com/guide/components/intents-filters.html

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
-1

Why would you pass a hashmap to an Activity? This makes NO Sense.

Take the data from the hashmap/preference and pass it in the extras from Activity A to Activity B. Stick to primitives when it comes to passing data between activities.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
-2

If your project allows it, just make the HashMap static then you can retrieve it in any other activity in your project by calling Class.hashmapvar

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • It solve some cases, but it'll always be a huge source of bugs. To understand why it shouldn't be done, please read: http://stackoverflow.com/questions/9541688/static-variable-null-when-returning-to-the-app – Italo Borssatto Nov 08 '13 at 11:29
  • of course you have to do a null check... this should be a quick solution to the problem, I know there are other ways to handle this, I was just intending what could be done – Nickolaus Nov 08 '13 at 11:53
  • A null check will just avoid a NullPointerException and you'll not get the HashMap you want (in some cases). – Italo Borssatto Nov 08 '13 at 15:55