0

I'm developing an application where I want to find out what volume the user had when he started my app from a method thats not onCreate(). I have created an int based on the current volume inside my onCreate but since it can't return anything I don't know how to get my int from there. It's very important that I use the int I generated at in onCreate().

How can this be done?

SweSnow
  • 17,504
  • 10
  • 36
  • 49

2 Answers2

0

If I well understood you, you should use: SharedPreferences to save that value.

pawegio
  • 1,722
  • 3
  • 17
  • 29
0

@pawegio right, if you want to use same vol level what user set up, use preferences. This is how to:

WRITE into preferences

SharedPreferences pref =
            context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);

    /*
     * Get the editor for this object. The editor interface abstracts the implementation of
     * updating the SharedPreferences object.
     */

    SharedPreferences.Editor editor = pref.edit();

    /*
     * Write the keys and values to the Editor
     */

    editor.putInt("VolumLevel", 60);
    /*
     * Commit the changes. Return the result of the commit.
     */

    e.commit();

READ from preferences

 SharedPreferences pref = context.getSharedPreferences("MyAppPreferences", MODE_PRIVATE);
 int volLevel = pref.getInt("VolumLevel", 50 /*Default if value wasn't setup yet*/);

 return volLevel;
Maxim
  • 4,152
  • 8
  • 50
  • 77