简体   繁体   English

Android保存用户登录退出状态

[英]Android save user login logout state

I need a little help again with Android SharedPreference.I was earching for a way to save the user state if he is logged in or not..So I did this part, but now the problem is something else.I have a tabhost which include a few tabs and the Login page is started from one of the child Activities.Here is a little example : 我再次需要Android SharedPreference的帮助。我想寻找一种方法来保存用户状态(无论他是否登录)。所以我做了这部分,但现在问题出在其他地方。我有一个tabhost,其中包括几个选项卡,从一个子活动开始登录页面,这是一个小例子:

TAB1 - TAB2 - TAB3 TAB1-TAB2-TAB3

TAB1 --> ACT1 (child activity of TAB1) ACT1 ---> Login Page. TAB1-> ACT1(TAB1的子活动)ACT1 --->登录页面。

So I'm using SharedPreferences to get and set the isLoggedIn states,but the problem is that I want to reload the UI when I close the Login Page, so TAB1 can show the new element. 因此,我正在使用SharedPreferences来获取和设置isLoggedIn状态,但是问题是我想在关闭登录页面时重新加载UI,以便TAB1可以显示新元素。

Here is my code for now : 这是我现在的代码:

Login Page : 登录页面:

SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = isLogged.edit();
        editor.putBoolean("isLoggedIn", true);
        editor.commit();
        this.notifyAll();

and in TAB1 I have this : 在TAB1中,我有:

SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean isLoggedIn = isLogged.getBoolean("isLoggedIn", false);

Any idea how to fix that? 任何想法如何解决?

As I understand your problem you want to recongnize when the user logged into on your "LoginActivity" so that you can reload the "TabActivity" right? 据我了解您的问题,您想在用户登录“ LoginActivity”时重新确认一下,以便您可以重新加载“ TabActivity”吗? You can do this by returning a result to the parent activity. 您可以通过将结果返回到父活动来做到这一点。

public class StackOverflowActivity extends Activity {

    public static final int LOGIN_REQUEST = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(StackOverflowActivity.this, Login.class);
                startActivityForResult(i, LOGIN_REQUEST);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case LOGIN_REQUEST:
            // TODO implement UI refresh
            Toast.makeText(getApplicationContext(), "I am coming from the login activity!",
                    Toast.LENGTH_LONG).show();
            break;
        default:
            Toast.makeText(getApplicationContext(), "Unexpected request code!",
                    Toast.LENGTH_LONG).show();
            break;
        }

    }
}

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

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