Put into shared preferences code:
PreferenceManager.getDefaultSharedPreferences(Bookmarks.this).edit()
.putString("togoto", link).apply();
String togoto = PreferenceManager.getDefaultSharedPreferences(Bookmarks.this)
.getString("togoto", "no");
Toast.makeText(Bookmarks.this, "Will go to " togoto, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_proxy_browser);
Intent myIntent = new Intent(Bookmarks.this, ProxyBrowserActivity.class);
startActivity(myIntent);
Get from shared prferences (In other context):
String togoto = PreferenceManager.getDefaultSharedPreferences(ProxyBrowserActivity.this).getString("togoto", "nogo");
Toast.makeText(ProxyBrowserActivity.this, "1 Going to " togoto, Toast.LENGTH_SHORT).show();
if (togoto != "no") {
Toast.makeText(ProxyBrowserActivity.this, "Going to " togoto, Toast.LENGTH_SHORT).show();
PreferenceManager.getDefaultSharedPreferences(ProxyBrowserActivity.this)
.edit().putString("togoto", "no").apply();
} else {
// no shared preferences
}
This does not appear to work and togoto returns no on the other context. How do I allow it to work over multiple contexts?
CodePudding user response:
Please use
getSharedPreferences(KEY, MODE_PRIVATE) rather than
PreferenceManager.getDefaultSharedPreferenceManager.
Your code is fine when you replace this.
CodePudding user response:
If u create a default SharedPreference, it' name is context.getPackageName() "_preferences". And getDefaultSharedPreferences get that. When u set a name like "MyApp_preferences", getDefaultSharedPreferences will find nothing. Use getSharedPreferences.
