I have manually created a Preferences Activity, where the checkbox data will be sent to a different Activity. When I send it, it is right. But when I receive it, I need to know if it is true or false, and I always get false.
Here's my code to send:
@Override
public void onBackPressed() {
if(checkBox.isChecked())
{
String trueThumb_check = "checked_thumb";
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putExtra("trueThumbnail", trueThumb_check);
Toast.makeText(Preferences.this, "check prefs", Toast.LENGTH_SHORT).show();
startActivity(i);
killAc();
}
else
{
String falseThumb_check = "not_check_thumb";
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putExtra("trueThumbnail", falseThumb_check);
Toast.makeText(Preferences.this, "not check prefs", Toast.LENGTH_SHORT).show();
startActivity(i);
killAc();
}
}
And to receive:
try {
SharedPreferences thumb_check = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
//To read preferences
String thumb_thumb = thumb_check.getString("trueThumbnail", "checked_thumb");
if (thumb_thumb.contains("checked_thumb")){
Toast.makeText(MainActivity.this, "checkBox ativo", Toast.LENGTH_SHORT).show();
} if(thumb_thumb.contains("not_checked_thumb")) {
Toast.makeText(MainActivity.this, "checkBox não ativo", Toast.LENGTH_SHORT).show();
}
SharedPreferences.Editor editor = thumb_check.edit();
editor.apply();
} catch (Throwable e) {
Toast.makeText(MainActivity.this, "Null checkBox", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
I believe the problem might be here:
String thumb_thumb = thumb_check.getString("trueThumbnail", "checked_thumb");
I always get a single " checked_thumb", but I don't know how to get "thumb_thumb" for both. I've tried sending different keys and values (because both are under the "trueThumbnail" key) but I still get true only.
Your receiving code is wrong. When you're sending the data you're sending through a intent, there's nothing wrong in there but when you're trying to receive you are getting from the SharedPreferences
So to access the data instead of this
SharedPreferences thumb_check = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
It should be this
Intent thumb_check = getIntent();
String thumb_thumb = thumb_check.getStringExtra("trueThumbnail", "checked_thumb");
A rewrite of the receive code
try {
// Get the intent passed through activities
Intent thumb_check = getIntent();
// Get the string in the extra
String intent_Thumb = thumb_check.getStringExtra("trueThumbnail", "checked_thumb");
// Get the shared preferences
SharedPreferences prefs = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
//To read from preferences
String pref_Thumb = prefs("trueThumbnail", "checked_thumb");
// preform what ever you want
} catch (Throwable e) {
Toast.makeText(MainActivity.this, "Null checkBox", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
You're mixing the SharedPreferences and Intents and it seems that you are lost in that code Make sure you read the android documentation it helps a lot. https://developer.android.com/guide/components/intents-filters.html
EDIT Responding your comment
SharedPreferences sharedPref = getSharedPreferences("ch4an.ytheloader", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("trueThumbnail", intent_Thumb);
// Or if you want to put a bool assuming that you have a
// variable called intent_Thumb_as_boolean as a boolean
editor.putBoolean("trueThumbnail", intent_Thumb_as_boolean);
editor.commit();
Check the android docs for the SharedPreferences
https://developer.android.com/training/data-storage/shared-preferences.html