简体   繁体   中英

Android - Maintain session after login without getting logout

I am creating an activity with name - "MainActivity.class" in which i am doing login and in next activity, i am doing logout.

After getting logged, i am maintaining session using sessionmanager.class in which i am using the method checklogin().

In the mainactivity, i am using session.checklogin() method due to which app is not starting due to heap problem. If I don't use the above method, the session is not getting maintained.

Please assist me in maintaining the session.

Here is the code for checklogin() from the sessionmanager class -

 public void checkLogin() {
    // Check login status
    if (this.isLoggedIn()) {
        // user is logged in redirect him to Login Activity
        Intent i = new Intent(_context, Home.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Staring Login Activity
        _context.startActivity(i);
    } else 
        if (!this.isLoggedIn()) {
        // user is not logged in redirect him to Login Activity
        Intent i = new Intent(_context, FirstActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Staring Login Activity
        _context.startActivity(i);

    }

}

In the MainActivity.class, I am using -

SessionManager session;

in onCreateMethod(), i am doing - session = new SessionManager(getApplicationContext()); then passing session.checklogin();

Three Points need to be cleared.

1.Sessions can be managed by use of Shared Preferences in android. It can be used to store datas as key value pairs.
2.Once your application finishes server communication then there wont be any sessions to make it alive.
3.SessionManager class is not actually used for maintaining sessions. It manages the media sessions as a queue.

See the following documentations. It would be helpful.
Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
SessionManaget http://developer.android.com/samples/MediaRouter/src/com.example.android.mediarouter/player/SessionManager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM