Shared Preferences And Onpause Doesn't Work
if i check a checkbox and go on tab without list view with checkbox and came to previous tab, i see checkbox checked previously...but if i click on another tab with list view with
Solution 1:
Your holder in method onPause(MyListFragment) was not instantiated. So when it came to the line
holder.chkBox.setChecked(true);
That exception would come out
Solution 2:
java.lang.NullPointerException means you are trying to use a component that is not initialized.
here, your motive is setChecked/unChecked of checkbox into PlanetAdapter
from onPause()
callback. You can do in this way:
- Create an Interface that will communicate between fragment to your adapter.
- In your
onPause
callback send command to your adapter for check/uncheck of your checkbox list.
Example: Suppose your interface is like it:
publicinterfaceAdapterCallback {
voidonCheckboxCallback(boolean isChecked);
}
In your Adapter implements AdapterCallback
Interface. Then you will get onCheckboxCallback()
method.
@OverridepublicvoidonCheckboxCallback(boolean isChecked) {
// Do your checkbox check/uncheck here
}
In this method you can checked/unchecked based on your isChecked value.
Now, in your Fragment/Activity that populating PlanetAdapter, just call this method
if (mCheckBoxValue) {
plAdapter.onCheckboxCallback(true);
} else {
plAdapter.onCheckboxCallback(false);
Post a Comment for "Shared Preferences And Onpause Doesn't Work"