SharedPreferences access without context

I read the question: this one and this one about reading general settings. But they still need a Context to access SharedPreferences. I want to know how to access SharedPreferences without context. thanks in advance

+5
source share
2 answers

I solve my problem by first extracting ApplicationContext ( this ) and then using this context to get SharedPreferences. thanks K-ballo.

+5
source

Application Class:

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {

    private static Context mContext;

    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }

    public static Context getAppContext() {
        return mContext;
    }

}

Announce the application in AndroidManifest:

<application android:name=".MyApplication"
    ...
/>

Using:

PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext());
0
source

All Articles