0

I am making an app in which I have two switch button,one for twitter login and other for twitter login.

I have gone through other posts on the same topics ( first, second)

but nothing is helping me,that's why posting a new question.

So basically am having a switch button to login and logout. It's working perfectly fine. If I close the app and again return back to the app, that too is working fine i.e session is kept intact but the status button is set to false i.e inactive.

I followed other answers and if I implement that, I get an error that Caused by: java.lang.IllegalArgumentException: Callback must not be null. (for twitter login).

I'll post some code's,to explain the situation.

tSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {

                if(isChecked){
                    SharedPreferences.Editor editor = getSharedPreferences("com.example.anubhaw.socialtwitter", MODE_PRIVATE).edit();
                    editor.putBoolean("service_status", tSwitch.isChecked());
                    editor.commit();
                    loginButton.performClick();
                }else{
                    Twitter.getInstance();
                    Twitter.logOut();
                    twitter_layout.setVisibility(View.GONE);
                    Toast.makeText(getApplicationContext(), "Logged out from Twitter", Toast.LENGTH_SHORT).show();
                }

            }
        });

 SharedPreferences prefs = getSharedPreferences("com.example.anubhaw.socialtwitter", MODE_PRIVATE);
    boolean tState = prefs.getBoolean("service_status", false);

    if(tState){
        //Do your work for service is selected on
        Toast.makeText(getApplicationContext(), "Inside the sharedPref", Toast.LENGTH_SHORT).show();
        tSwitch.setChecked(true);
    } else {
        //Code for service off
    }

If I remove the sharedPref thing,everything is working fine,other than the state of that switch button.

Please let me know if you need any more info. Thanks

Community
  • 1
  • 1
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • Where does the code go wrong? I still don't get it. – Blundell Dec 19 '15 at 19:51
  • I have two switch buttons, one for facebook login and other for twitter login. I have two frame layout too(one for each,where I show basic information and allow user to tweet and share/like). My problem is that, if I close the app and start it again,I can get the session back but the switch button is in "off" stage(If I don't implement `SharedPreferences` thinga,and the error after implementing SharedPreference is `Callback must not be null.`.(1/2) – driftking9987 Dec 19 '15 at 20:13
  • I believe this is so because it's calling `onCheckChanged` function(as am changing the status of the same. If I remove the `SharedPreferences` preference thing,it's working fine. I have a function to check the active session of twitter too.(2/2) – driftking9987 Dec 19 '15 at 20:13
  • show us the full log that says `Callback must not be null.` i.e. what line is saying this? – Blundell Dec 19 '15 at 22:12
  • Thanks. But I got it working after tweaking mahmoud's answer. I had to make some changes in that. Again, thanks. – driftking9987 Dec 20 '15 at 21:09

2 Answers2

0

you can use below code:

sharedPreferences = getSharedPreferences("com.example.anubhaw.socialtwitter", MODE_PRIVATE);
    preferences = sharedPreferences.edit();
    tSwitch.setChecked(sharedPreferences.getBoolean("isChecked",false));
    tSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                preferences.putBoolean("isChecked",true);
            }else {
                preferences.putBoolean("isChecked",false);
            }
            preferences.commit();
        }
    });
mahmoud moustafa
  • 223
  • 3
  • 11
  • Thanks. I had to do some changes, But I got it working. Few questions, for `sharedPreferences` you used small `s`, we need `S` (i am wondering if that's intentional and I was supposed to declare it somewhere?). Then in the second line you used `preferecnces` which you didn't declare anywhere. This answer pointed me in right direction. Thanks a lot. – driftking9987 Dec 20 '15 at 21:12
  • just a side point.. you dont need to make a if else just preferences.putBoolean("isChecked",isChecked); will work. – Shia G Dec 23 '15 at 20:57
0

The code by Mahmoud doesn't work and needs some changes. Here is a working version. It basically saves the toggle in SharedPreferences.

private Switch mySwitch;


 // Switch
    mySwitch = (Switch) findViewById(R.id.mySwitch);

    boolean value = true; // default value if no value was found

    final SharedPreferences sharedPreferences = getSharedPreferences("isChecked", 0);

    value = sharedPreferences.getBoolean("isChecked", value); // retrieve the value of your key
    mySwitch.setChecked(value);

    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                sharedPreferences.edit().putBoolean("isChecked", true).apply();
            }else {
                sharedPreferences.edit().putBoolean("isChecked", false).apply();;
            }
        }
    });