Search code examples
androidandroid-fragmentsandroid-preferences

How to call a method from static PreferenceFragment using onPreferenceClick


I'm using Android studio settings template (that uses static PreferenceFragment within AppCompatPreferenceActivity). I am trying to listen to a preference click in order to call a method that opens a file picker. I get an error saying:

non-static method pickImage(int) cannot be referenced from a static context". I'm not sure how to do it correctly. Should I just change all methods called to Static?

Relevant parts of the code:

public class SettingsActivity extends AppCompatPreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
        setTheme(R.style.PreferencesStyle);
        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();
    } 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class GeneralPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            setHasOptionsMenu(true);            

            Preference correctAnswerPickImagePref = findPreference(Constants.CORRECT_ANSWER_PICK_IMAGE_PREF);
            correctAnswerPickImagePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                public boolean onPreferenceClick(Preference preference) {
                    pickImage(Constants.PICK_CORRECT_ANSWER_IMAGE_REQUEST);
                    return true;
                }
            });
        }  


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
        }
    }

    private void pickImage(int requestCode) {
        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    requestCode);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            // Permission has already been granted
            startPickImageActivity(requestCode);
        }
    }
}

Solution

  • Solution 1: Move private void pickImage(int) method into GeneralPreferenceFragment, then fix error caused by context.

    Solution 2: Make private void pickImage(int) method static, and add a Context argument to it, then fix error caused by context.