开发者

Two radio buttons wether to keep the user's credentials or not [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

I'm working on having a "Keep me on Logged in" state on my app. How should i do it?

The current codes are working, what I want is, when the user checks the radio button off. I want the app to go back to the log in screen when the user's reopens the app.

Here's the java file:

@Override
    public void onCreate(Bundle savedInstanceState) {
        开发者_JAVA技巧super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);

        final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
        final RadioButton radio_off = (RadioButton) findViewById(R.id.off);

        SharedPreferences pref = getSharedPreferences("LoggedIn", MODE_PRIVATE);
        final SharedPreferences.Editor prefEdit = pref.edit();

        radio_on.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                prefEdit.putBoolean("booleanValue", isChecked).commit();
          }
     });


as your previous question your are too near to your solution well try this

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

    final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
    final RadioButton radio_off = (RadioButton) findViewById(R.id.off);

    SharedPreferences pref = getSharedPreferences("LoggedIn", MODE_PRIVATE);
    final SharedPreferences.Editor prefEdit = pref.edit();

    if(pref.getBoolean("booleanValue", "")){
       intent = new Intent(getApplicationContext(),HomeActivity.class);
       startActivity(intent);
       finish(); 
    }

    radio_on.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            prefEdit.putBoolean("booleanValue", isChecked);
            prefEdit.commit();
      }
 });


From what I understand, when a user gets to the first activity(loginactivity), if he chooses to login automatically, when he opens the app again he is loged in and can't change his option , not to login, because he never gets to that screen. If this is correct, this is a problem of application design. You could just add a setting in the app that lets you change your option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜