I saw related posts like this - Disable the Asynchronous display of a Toast [duplicate] and I tried cancel() method from there, but they don't solve my problem. Maybe it is because some difference related with Preference Activity.
Please, help me improve my code so the Toast displays right after click.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_screen);
Context context = getApplicationContext();
settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences settings, String key) {
Toast theToast;
theToast = Toast.makeText(this, "toast text", Toast.LENGTH_LONG);
if(key.equals("checkbox_key")){
theToast.show();
}
if (key.equals("checkbox_key")&& theToast != null){
theToast.cancel();
}
}