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

Asked

Viewed 68 times

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 am unable 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 whenever the user enters the Mainactivity screen, save the status that was sent from the preferences Activity.

1 answer

0

Let me get this straight, 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’ve helped!

  • I can already pass the status of the checkbox. So I received it correctly in the other Activity. Now I need to SAVE and (update) the status of the same.

  • For whenever the user enters the Mainactivity screen, stay saved the status that was sent from the preferences Activity. Got it?

  • Is emitting some error when you try to save in preferences?

  • Actually, no. But when I have the code to save and restart the app, Toast does not appear. Got it? Being that he was supposed to have saved the String and have come back to find out if it contains "checked_thumb" in the same.

  • Thus, successively appeared the Toast, if had saved the String and ascertained.

  • In the preferences activity everything is ok! I no longer need to save there. I want to save in Mainactivity. The activity that was received to String.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.