0

In one of my views the user makes a selection. I want to store that selection in a variable I can use across all views so it can be used for further database queries such as "bookId".

How can I make "bookId" a global variable that is set on one view and can be accessed across the other views when needed?

----- Edit: What I'm attempting to do based on comments and answers -----

On my main activity where the SharedPreference is stored I have this before the onCreate:

public static final String PREFS_NAME = "myPrefs";
SharedPreferences settings;
Integer bookId;

In my onCreate I've done this:

settings = getSharedPreferences(PREFS_NAME, 0);
bookId = settings.getInt("bookId", 0);

After the button press I'm storing a custom attribute and attempting to set bookId in the SharedPreference:

SharedPreferences.Editor editor = settings.edit();
editor.putInt("bookId",bookKey);
editor.commit();

In another view I'm attempting to get the bookId from the SharedPreference and, for testing purposes, I'm trying to set the stored value to a textView just to make sure it stored and carried over correctly.

Before the onCreate on the second view:

public static final String PREFS_NAME = "myPrefs";
SharedPreferences settings;

Inside the onCreate:

settings = getSharedPreferences(PREFS_NAME, 0);
Integer bookId = settings.getInt("bookId", (Integer) null);
tempBookTextView = (TextView) findViewById(R.id.tempBookTextView);
tempBookTextView.setText(bookId);

I have a two questions, how does this look so far? Any ideas why the app crashes when I use

Integer bookId = settings.getInt("bookId", (Integer) null);
dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • 2
    Consider using SharedPreference? I have used that in a recent app. You can set a key-value pair in one activity and get that value in other activities. – Taslim A. Khan Nov 27 '13 at 05:29
  • What is a good resource to learn how to use SharedPreference. (I'm still learning). – dcp3450 Nov 27 '13 at 05:55
  • http://developer.android.com/guide/topics/ui/settings.html Jump to the bit on preferences. – psyren89 Nov 27 '13 at 06:01
  • Application class is more useful than SharedPreferences if that Variable changes value from time to time. – Dhaval Nov 27 '13 at 06:08
  • @dcp3450 I have recently added an answer to another question. Check this [link](http://stackoverflow.com/questions/20005051/writing-shared-preference-file-for-only-one-radio-button-under-radio-group). If it helps, let me know, I will post this as an answer :-) – Taslim A. Khan Nov 27 '13 at 06:09
  • edited my question to account for comments and answers – dcp3450 Nov 27 '13 at 15:55

3 Answers3

0

To kill the spirit of encapsulation,

public class Globals {
    public static int x;
    public static int y;
}

and later

if (Globals.x == 0) { ... }

But please don't do exactly that, any class can contain static variables, find a class responsible for the value you want to store.

OTOH, android processes may be restarted when you don't expect it, in which case all the variables will be reset. So it's better to use shared preferences and if they don't work as expected (which I have seen in at least one release of Android), store the instance of shared preferences in a static variable.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

Instead of using global variable to access variable value through out the app try using SharedPreferences.

sample activity:

public class Book extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";
     String mBookId = null;
    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       mBookId = settings.getString("book_id", null);
       // use book_id;
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("book_id", mBookId);

      // Commit the edits!
      editor.commit();
    }
}

Preferences are typically created private and can be accessed via all over the application components. Sharing data with other application with world readable or writable preference file is rarely used, as the external component would need to know the exact filename and location of the file.

Pradip
  • 3,189
  • 3
  • 22
  • 27
  • `(Integer) null` replace this with 0, app will not crash. Null can't be casted to integer. – Pradip Nov 27 '13 at 18:02
  • that was it! In case anyone comes across this question, I used this answer/example along with this video: http://www.youtube.com/watch?v=wG3XPdVPsxU – dcp3450 Nov 27 '13 at 18:32
0

You can use Shared Preferences

Saved at once !

SharedPreferences.Editor editor =  PreferenceManager.getDefaultSharedPreferences(ProjectAct.this).edit();
editor.putInteger(StaticVariable.BOOKID, "1");
p.get("contract_no"));
editor.commit();

Then call from anywhere like that

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int book_id = prefs.getInteger(StaticVariable.BOOKID,null);

See more at How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Thein
  • 3,940
  • 2
  • 30
  • 34