C.S.
C.S.

Reputation: 379

Toggle button not working the second time is used

this is my code:

public void gotoalbanian (View view) {
    if (toggleButton.isChecked()) {
        SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("selectedlanguage", "Albanian");
        editor.commit();
        Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " is the default page", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.browser);
        WebView myWebView = (WebView) findViewById(R.id.webView2);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.example.com");
    }
    else {
        setContentView(R.layout.browser);
        WebView myWebView = (WebView) findViewById(R.id.webView2);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.example.com");
    }
}

And this is the return button:

public void gotomain (View view) {
        SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        if (sharedPref.getString("selectedlanguage", null) != null) {
            Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " no longer default", Toast.LENGTH_SHORT).show();
        }
        WebView myWebView = (WebView) findViewById(R.id.webView2);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.destroy();
        editor.clear();
        editor.commit();
        setContentView(R.layout.content_main);
    }

User can toggle a button so the app will save the selection, when I launch it and toggle the button so it will save, works fine but if I go back with gotomain and enable or disable the toggle button the outcome wont change, app will act however the button was before gotomain was used. If was enabled, will act enabled even if you disable it and vise versa.

Thank you.

Upvotes: 0

Views: 240

Answers (2)

Jörn Buitink
Jörn Buitink

Reputation: 2916

setContentView should only called once per Activity. Create your contentView in your onCreate function. Save a reference to your WebView as a property (and save the checked state, too). Maybe try something like this:

private boolean isChecked = false;
private WebView myWebView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.browser);
    myWebView = (WebView) findViewById(R.id.webView2);
    toggleButton = ??;
}

public void onResume() {
    toggleButton.setChecked(isChecked);
}

public void gotoalbanian (View view) {
    isChecked = toggleButton.isChecked();
    if (toggleButton.isChecked()) {
        SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("selectedlanguage", "Albanian");
        editor.commit();
        Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " is the default page", Toast.LENGTH_SHORT).show();
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.example.com");
    } else {
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.example.com");
    }
}

public void gotomain (View view) {
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    if (sharedPref.getString("selectedlanguage", null) != null) {
        Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " no longer default", Toast.LENGTH_SHORT).show();
    }
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.destroy();
    editor.clear();
    editor.commit();
}

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

it is not working because you are providing a new layout to setContentView, (setContentView(R.layout.browser);). This will create a new View for your Activity making the hold reference not valid anymore. What you can do is to add the WebView to your "main" layout, the one you set in onCreate and change the visibility of its components accordingly to the state of your toggle button. If you have more complex use cases then, you should use Fragments

Upvotes: 2

Related Questions