Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

android - Login Session - Activities

I have a login in my application. I want to know the best way to set a global session or equivalent that I can refer to from any activity so that they will know if a user is logged in. Also, so that other activities can get the user id to do database transactions.

I don't want to use putExtra() as I will have to do it for every Intent I build across the application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create an Application class which can be used as a global state holder.

Here's some sample code:

public class SampleApplication extends Application {

    private static String username;
    private static String password;

    @Override
    public void onCreate() {
        super.onCreate();
        username="";
        password="";
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        SampleApplication.username = username;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        SampleApplication.password = password;
    }

}

After declaring static methods and variables, you should define your application class in your AndroidManifest.xml. Use the android:name attribute of the application tag in order to define your class name.

Here's some more sample code:

<application 
    android:name=".SampleApplication"
    android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SampleApp"
              android:label="@string/app_name"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Your application class will share all the life cycle events as that of Activity and will get destroyed when the application exits.

You can access your application variables with static getters and setters from any activity or service:

SampleApplication.setUsername("");
String currentUserName=SampleApplication.getUsername();
SampleApplication.setPassword("");
String currentPassword=SampleApplication.getPassword();

You can also, rather than going for singleton class, opt for the normal class of Application

Alternative to the same example:

public class SampleApplication extends Application {

    private String username;
    private String password;

    @Override
    public void onCreate() {
        super.onCreate();
        username="";
        password="";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        SampleApplication.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        SampleApplication.password = password;
    }

}

You can access these with:

((SampleApplication)getApplication()).getUsername();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...