0

What i want to achieve is to tick the checkbox and when leaving the activity and returning the tick should stil be there. However now i tick it and when going to a different activity and returning the tick is gone. How can save the tick in the check box?

Here is my code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_tick, container, false);

    CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.checkBox62);
            if(checkBox.isChecked())
             checkBox.setChecked(false);
            else 
             checkBox.setChecked(false);

            return rootView;
  }
}

Any help will be appreciated.

wemapps
  • 111
  • 8
  • Strore check box value in sharedpreferences. And when come to this screen first check store value. –  Nov 05 '14 at 13:49
  • Could you possibly show me how to implement this? – wemapps Nov 05 '14 at 14:16
  • Read this http://stackoverflow.com/questions/23024831/android-shared-preferences-example http://stackoverflow.com/questions/14292984/using-shared-preference-save-5-check-box-value-in-a-arraylist –  Nov 05 '14 at 14:40

2 Answers2

0

You need persistent data storage to maintain the state.

In the code you posted the value of the checkbox is only stored while the view is alive, as a consequence you will lose that data when you leave the activity.

You can: Add a variable in the Application class Add a preference Or any of the miriad other solutions out there

The main idea is that you have to differentiate between data and view. Views only store data per instance, you need a backend to maintain it.

dzsonni
  • 194
  • 1
  • 13
0

The states of UI components are automatically saved by Android OS in the Bundle named savedInstanceState (parameter of the onCreateView() method). However, you can add custom data to this Bundle in overriding the onSaveInstanceState() method.

Also, your code is wrong : you're putting your Checkbox to the uncheck state. And to finish this part of your code is useless since it's already managed by Android. Try to remove it completely or to fix it if you really want to keep it.

if(checkBox.isChecked())
    checkBox.setChecked(false); // Should be true to be algorithmically correct
else 
    checkBox.setChecked(false);

Sources

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

mithrop
  • 3,183
  • 2
  • 20
  • 39