Android - Save and update String sent from one activity to another

0

// I can send and receive two different values in one

@Override
    public void onBackPressed() {
        if(checkBox.isChecked())
        {
            String trueThumb_check = "checked_thumb";
            Intent i = new Intent(getBaseContext(), MainActivity.class);
            i.putExtra("trueThumbnail", trueThumb_check);
            startActivity(i);
            killAc();
        }
        else
        {
            Intent i = new Intent(getBaseContext(), MainActivity.class);
            i.putExtra("falseThumbnail", "");
            startActivity(i);
            killAc();
        }

    }

// Then I received different values:

    try {
                Intent thumb_check = getIntent();
                String thumb_thumb = thumb_check.getStringExtra("trueThumbnail");
                if (thumb_thumb.contains("checked_thumb")){
                Toast.makeText(MainActivity.this, "checkBox ativo", Toast.LENGTH_SHORT).show();
                } else {
                Toast.makeText(MainActivity.this, "not checkBox", Toast.LENGTH_SHORT).show();
                }
            } catch (Throwable e) {
                e.printStackTrace();
}

// But I'm not able to save the result. It needs to be saved and updated when the checkBox is checked or not

SharedPreferences sharedPref = getSharedPreferences("ch4an.ytheloader", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("trueThumbnail", thumb_thumb);
editor.commit();

EDIT: For every time the user enters the MainActivity screen, the status that was sent from the preferences activity is saved.

    
asked by anonymous 12.12.2017 / 18:35

1 answer

0

Let me see if I understand, you need to pass the status of CheckBox to another activity!

Try this:

Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putBooleanExtra("checkBoxValue", checkBox.isChecked());
startActivity(i);
if (getIntent().getBooleanExtra("checkBoxValue")){
   Toast.makeText(MainActivity.this, "checkBox ativo", Toast.LENGTH_SHORT).show();
} else {
   Toast.makeText(MainActivity.this, "checkBox não ativo", Toast.LENGTH_SHORT).show();
}

I hope I have helped!

    
12.12.2017 / 20:14