1

I am still new to android app development.

I have a table in QSLite database that gives me key parameters about each user. A typical table is attached as image below;

enter image description here

When a user logs into the application, i would like to set certain constants that i can use throughout the entire application. For example, if a user with UserID = 3 in the above table logs in, i want to get hold of this UserID (CurrentUserID). I would then use that ID to check from anywhere in the app activity whether he is from Location 2, or which DataSourceID he coming from. The result will determine which type of activity or permissions i should display/give to the user.

In Access, we used global variables. However, from what i read in java, the use of public static variables (equivalent) is not recommended.

Could you point me to some examples of how challenges like this are solved in Java?

Sheridan
  • 68,826
  • 24
  • 143
  • 183
user3267567
  • 524
  • 4
  • 14
  • @Darkhogg, an edit is supposed to add value to a post... adding a tag to a title is *not* an improvement - in fact, you will find that a number of users set about removing tags from titles because they are already in the tags, which are taken into consideration when someone searches for particular terms. Please see the [Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) post on StackOverflow Meta for more information on this. – Sheridan Mar 04 '14 at 13:19
  • @Sheridan Adding a tag or removing the already-present one would have been equally useful: After reading the title, I dind't even look at the tags. And in this particular case, being an Android question **is** relevant. Sorry for the inconvenience, but *if Java is in the title, then Android should be in the title too*. – Darkhogg Mar 04 '14 at 13:37
  • I'd argue that neither adding, nor removing a tag from the title would be useful... neither adds anything to the post, which is what edits are really for. – Sheridan Mar 04 '14 at 13:50
  • many thanks to all of you for sparing your precious time to point me in the right direction. – user3267567 Mar 04 '14 at 17:21

5 Answers5

1

Create a singleton object and populate it with the values

http://en.wikipedia.org/wiki/Singleton_pattern

Mark Wagoner
  • 1,729
  • 1
  • 13
  • 20
  • http://stackoverflow.com/questions/11292109/why-is-singleton-considered-an-anti-pattern-in-java-world-sometimes – Darkhogg Mar 04 '14 at 13:01
1

You only need to use SharedPreferences: http://developer.android.com/guide/topics/data/data-storage.html#pref

a person
  • 986
  • 6
  • 13
1

You can extend the Application class - which IS a singleton - and keep your variables as fields or properties there

in you manifest you define such class as:

<application
    android:name="com.something.SingletonApplication"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Note that `Application` is not a singleton in itself (no `getInstance()`, no way of preventing mltiple instances...), but the way Android treats it makes it a singleton-like class. – Darkhogg Mar 04 '14 at 13:08
  • lets say in this way: the android life-cycle management makes is to a singleton (if you define it in the manifest). You can instantiate it of course somewhere else on your own risk :) – injecteer Mar 04 '14 at 13:11
0

You can define class with static fields to store your global values for example

class Globals {
   public static int var1;
   public static String var2;

   //and so on
}

You can access this variables in this way

Globals.var1 = 1;
Globals.var2 = 2;

If you want variable to be constants add "final" to any variable declaration

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
0

If you want to ensure that yours variables can be used during whole application lifecycle you can extend Application class

public class MyApplication extends Application
{

  private int userId;

  @Override
  public void onCreate()
  {
    super.onCreate();


  }
  public void setUserId(int id){
     this.userId = id;
  }
  public void getUserId(){
     return this.userId;
  }


  public void customAppMethod()
  {
    // Custom application method
  }

You can also make singleton class and keep reference into the class that extend application class, but this not really necessary in most of cases because singleton object will stay in memory until application receives memory warning.

Don't forget to add in your manifest file:

 <application android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:name="com.your.package.MyApplication">
rule
  • 5,657
  • 1
  • 16
  • 21