0

I have a fragment with a button that sets a background theme for the whole app. I have set up an interface so the fragment can tell the main activity to set the background or remove it depending on what button the user clicks.

The problem is that every time the app is opened the background isn't saved and needs to be toggled again. I have seen that this can be solved with SharedPreferences but implementing it here is confusing me

In my fragment This presents two buttons that send the values 1 or 2 to the main activity to toggle the background

enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.themechanged(2);
            enable.setVisibility(View.GONE);
            disable.setVisibility(View.VISIBLE);

        }
    });
    disable = (Button) rootView.findViewById(R.id.disable);
    disable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.themechanged(1);
            disable.setVisibility(View.GONE);
            enable.setVisibility(View.VISIBLE);
        }
    });

In my Main Activity This takes the value from the listener and toggles the background depending on what the value is

    @Override

    public void themechanged(int value) {

    if(value==2) {
        if (isDarkTheme) {
            appbackground.setVisibility(View.GONE);
            shade.setVisibility(View.GONE);
        } else {
            appbackground.setVisibility(View.VISIBLE);
            shade.setVisibility(View.VISIBLE);
        }
    }else if(value!=2||value==1){
            appbackground.setVisibility(View.GONE);
            shade.setVisibility(View.GONE);
    }
}
Alpoe
  • 277
  • 1
  • 4
  • 16

3 Answers3

1

Use SharedPrefence to store the value for theme like-:

Global Variable

SharedPreferences pref;
SharedPreferences.Editor editor;

In OnCreateView()

pref = getActivity().getSharedPreferences("Theme", Context.MODE_PRIVATE);
editor = pref.edit();

Now, store preferences on Button click

enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      editor.putInt("yourTheme", 2);
      editor.commit();
        listener.themechanged(2);
        enable.setVisibility(View.GONE);
        disable.setVisibility(View.VISIBLE);

    }
});
disable = (Button) rootView.findViewById(R.id.disable);
disable.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editor.putInt("yourTheme", 1);
        editor.commit();
        listener.themechanged(1);
        disable.setVisibility(View.GONE);
        enable.setVisibility(View.VISIBLE);
    }
});

and then, In OnCreate() of MainActivity you can check like

SharedPreferences pref = getSharedPreferences("Theme", MODE_PRIVATE);
value= pref.getInt("yourTheme", 1);//1 is default value

if(value==2) {
    if (isDarkTheme) {
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
    } else {
        appbackground.setVisibility(View.VISIBLE);
        shade.setVisibility(View.VISIBLE);
    }
}else if(value==1){
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
}

Done, it may be helpful

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
  • would it need to be SharedPreferences pref = getActivity().getSharedPreferences("Theme", MODE_PRIVATE); since it's in a fragment? – Alpoe Oct 11 '17 at 13:37
  • Getting a null pointer exception for some reason, checking my code – Alpoe Oct 11 '17 at 13:52
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.support.v4.app.FragmentActivity.getSharedPreferences(java.lang.String, int)' on a null object reference at (HomeFragment.java:94) line 94 is SharedPreferences pref = getActivity().getSharedPreferences("Theme", MODE_PRIVATE); – Alpoe Oct 11 '17 at 13:55
  • @Alpoe change MODE_PRIVATE to Context.MODE_PRIVATE as mentioned in edited code – UltimateDevil Oct 11 '17 at 13:58
  • no change. still null pointer exception in the same place. For some reason it works when it's declared in onClick, but then the app doesnt remember when you disable the background. – Alpoe Oct 11 '17 at 14:03
  • I think your getActivity() is returning null – UltimateDevil Oct 11 '17 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156478/discussion-between-ultimatedevil-and-alpoe). – UltimateDevil Oct 11 '17 at 14:11
  • Cheers man, it worked. I was actually just trying what you updated but I didn't do it for the editor as well so crashed. Working perfectly now. – Alpoe Oct 11 '17 at 14:13
0

In the onClick() you should do 2 things:

  • Sent the value to the listener (you're already doing this)
  • Save this value to the preferences (already posted how to do that here)

Then, in the onCreate() of your MainActivity you should check for that preference and do the same you are doing on themechanged(int)

Actually, you could use only one onClickListener(), this way:

// Not need to cast to `Button`, since all views can have an onClickListener
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)

// Put this as a member of your Fragment class.
View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.enable) {
            // Save your preference here
            // ... 
            listener.themechanged(2);
            enable.setVisibility(View.GONE);
            disable.setVisibility(View.VISIBLE);            
        }

        if (v.getId() == R.id.R.id.disable) {
            // Save your preference here
            // ... 
            listener.themechanged(2);
            disable.setVisibility(View.GONE);
            enable.setVisibility(View.VISIBLE);
        }
    }
}
Jorge E. Hernández
  • 2,800
  • 1
  • 26
  • 50
0

Let me share this more complex example which can cover this and future needs: https://gist.github.com/walterpalladino/4f5509cbc8fc3ecf1497f05e37675111 The PersistenceManager class is generic, all your app data should be included in the Settings class. I hope it helps.

Walter Palladino
  • 479
  • 1
  • 3
  • 8