mike_x_
mike_x_

Reputation: 1929

Android global variables in class extending application or not?

I want to create some global variables in my android application, for example when the user logs in i want to have a variable to hold the username.

Should i create a MyApplication extends Application and add the variables in there or should i just create a "MyGlobals" class with public static variables inside?

Which one is better and why?

Upvotes: 0

Views: 561

Answers (1)

josedlujan
josedlujan

Reputation: 5600

In most cases a class is extended, although other solutions exist. Extend the base android.app.Application class and add member variables:

public class MyApplication extends Application {

    private String yourVar;

    public String getYourVar() {
        return yourVar;
    }

    public void setYourVar(String yourVar) {
        this.yourVar = yourVar;
    }
}

Upvotes: 1

Related Questions