简体   繁体   English

注销时如何破坏活动?

[英]How to destroy the activity when logout?

I am developing apps in android. 我正在用android开发应用程序。 In that I have login and logout activity with session management using SharedPreferences. 这样,我就可以使用SharedPreferences通过会话管理进行登录和注销活动。 my apps logout , store data in sharedpreferences to maintain session successfully done. 我的应用程序注销,将数据存储在sharedpreferences中,以保持会话成功完成。 when I do logout from the apps(second activity) and get rendered to first activity(login activity ) but what happen is when i clicked back button on emulator after successfully logout still again rendered to second activity and some version like 2.3.3 etc encountered exception 当我从应用程序注销(第二个活动)并呈现为第一个活动(登录活动),但发生的情况是当我成功注销后仍再次呈现到第二个活动并遇到某些版本(如2.3.3等)时,在模拟器上单击后退按钮例外

java.lang.RuntimeException: Unable to destroy activity {com.oj.bs/com.oj.bs.LoginActivity}: java.lang.NullPointerException

can anybody fix the problem. 任何人都可以解决这个问题。 please refer following code 请参考以下代码

following is the login activity code 以下是登录活动代码

sessionmngr = new SessionManager(getApplicationContext());

                    JSONObject json_user = jsonObj.getJSONObject("user");
                    sessionmngr.createLoginSession(id,json_user.getString(KEY_UNAME), 
                            json_user.getString(KEY_UEMAIL));
                     .
                     .

following is the Session Management 以下是会话管理

public class SessionManager {

SharedPreferences pref;
Editor editor;
Context contxt;
//shared pref mode
int PRIVATE_MODE = 0;
 // Sharedpref file name
private static final String PREF_NAME = "bookingScapePref";

// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_NAME = "name";
public static final String KEY_ID = "id";
public static final String KEY_EMAIL = "email";

public SessionManager(Context context) {
    super();
    this.contxt = context;
    pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
    editor.commit();
}
/**
 * Create login session
 * */
public void createLoginSession(int id,String name, String email) {

    editor.putBoolean(IS_LOGIN,true);
    editor.putString(KEY_NAME, name);
    editor.putString(KEY_EMAIL, email);
    editor.putInt(KEY_ID, id);
    editor.commit();
}
/**
 * Get stored session data
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> userDetails = new HashMap<String, String>();
    userDetails.put(KEY_NAME, pref.getString(KEY_NAME, null));
    userDetails.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
    return userDetails;
}
/**
 * Check login method will check user login status
 * */
public void checkLogin() {

    if (!this.isLoggedIn()) {
        Intent i = new Intent(contxt, LoginActivity.class);
        //closing all the activity
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        contxt.startActivity(i);
    }
}

public void logoutUser() {
    //clearing all data from sharedPreferences
    editor.clear();
    editor.commit();
    Intent intnt = new Intent(contxt, LoginActivity.class);
    // Closing all the Activities
    intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Add new Flag to start new Activity
    intnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Staring Login Activity
    contxt.startActivity(intnt);
}

/**
 * Quick check for login
 * **/
// Get Login State
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
}

} }

this is second activity here i'm checking session 这是第二项活动 ,我正在检查会话

public class ProjectFragActivity extends SherlockFragmentActivity {

SessionManager sessionMngr ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sessionMngr = new SessionManager(getApplicationContext());

    Toast.makeText(getApplicationContext(), "User Login Status: " + sessionMngr .isLoggedIn(), 
            Toast.LENGTH_LONG).show();
    sessionMngr.checkLogin();
            .
            .
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
super.onBackPressed();
}

Hi, Use onBackpressed function in your login Screen and implement this Intent function. 嗨,在您的登录屏幕中使用onBackpressed函数并实现此Intent函数。 It will take you to the home screen of the phone. 它将带您进入手机的主屏幕。

Hope this will give you some solution. 希望这会给您一些解决方案。

A simpler solution should be to call finish(); 一个更简单的解决方案应该是调用finish(); in your second activity. 在您的第二项活动中。

First don't call finish() in destroy() , the activity is already finishing and going away, plus that screws up with everything else. 首先,不要在destroy()调用finish() ,该活动已经完成并消失了,而且还有其他所有问题。 What you need to do is call finish() upon logging out, whatever function you have to log the person out, at the end of it, put finish() . 您需要做的是在注销时调用finish() ,无论您要注销该人具有什么功能,都必须在其末尾添加finish()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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