About Shared Preferences...

Asked 2 years ago, Updated 2 years ago, 118 views

Please understand that there are many shortcomings as it is written on the phone If you tell me the weird part, I'll upload the material or reply to it with a computer laterㅜ<

public static SharedPreferences _Saved_Data = Context.getSharedPreferences("Settings", Context.MODE_PRIVATE);
public static SharedPreferences.Editor _Data_Edit = _Saved_Data.edit();

I'm going to make it like this and refer to it in another fragment Error continues at the beginning of getSharedPreferences In Zamarin, Application.Context was resolved How do I solve this problem in Android Studio? I can't find an answer at all

First of all, I'm sorry for not being able to explain it for a long time

So you want to create a separate class file to define a share there and call up the share from different class files But in C#, it worked well like above ApplicationContext is not working and nothing is working while moving to Java"T" The problem is that it doesn't have an error, but the moment you press the menu where you use it, it bounces...

sharedpreferences

2022-09-22 20:36

1 Answers

I'm not familiar with Xamarin, so I'm careful to answer, but it seems that you can use Context as a static variable in Xamarin.

First of all, there is no compilation error, so I think it would be helpful for you to attach the log when the app is dying.

In addition, when you need to create a Preference-related Util class, you usually write and use it as follows. If you have to use Preference as a static variable like in Xamarin, you have to use it as a static variable until Context, but it's not the way I recommend it.

public class PreferenceUtils {
    private static final String PREF_SETTINGS = "pref_settings";
    private static final String KEY_ALARM = "key_alarm";

    private PreferenceUtils(){ }

    public static boolean getAlarmOnOffValue(Context context) {
        final SharedPreferences pref = context.getSharedPreferences(PREF_SETTINGS, Context.MODE_PRIVATE);
        return pref.getBoolean(KEY_ALARM, false);
    }

    public static void setAlarmOnOffValue(Context context, boolean value) {
        final SharedPreferences.Editor editor = context.getSharedPreferences(PREF_SETTINGS, Context.MODE_PRIVATE).edit();
        editor.putBoolean(KEY_ALARM, value);
        editor.apply();
    }
}


2022-09-22 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.