'How to access a variable that's inside a static fragment, from outside this fragment?

I'm on Android. I need to access a variable that's inside a static fragment called SettingsFragment (which itself is inside an activity called SettingsActivity) from another Fragment called MyDialogFragment.

Here is the SettingsFragment. The variable I want to access is called drawable:

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {

        //This is the variable I want to access inside MyDialogFragment
        public Drawable drawable = AppCompatResources.getDrawable(getContext(), R.drawable.hohol);

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.preferences, rootKey);
        }

        @Override
        public void onDisplayPreferenceDialog(Preference preference) {
            if (preference instanceof MyDialogFragment) {
                MyDialogFragment myDialogFragment = new MyDialogFragment();
                myDialogFragment.show(getActivity().getSupportFragmentManager(), "My Dialog");
            }
        }
    }
}

And this is the MyDialogFragment:

public class MyDialogFragment extends AppCompatDialogFragment {

    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_dialog, null);
        builder.setView(view);

        Drawable drawable = SettingsActivity.SettingsFragment.drawable; //Doesn't work

        return dialog;
    }

}

I tried to get the drawable inside of the dialog fragment but I can't figure out how. Important thing to note is that I cannot make the drawable variable inside SettingsFragment a static variable because I need it to be non static.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source