Domanda

I am using checked views in my list. I have 2 toggle buttons,which when clicked update the UI. i want to make my code such that it remembers the previously selected value and if it remains the same done button is disabled and if it is changed ,say I switch to value 2, done button is enabled and vice versa and also it remembers my previous selection when I return to the same view later. Here's is my code for the same:

 public class ManageListDashboardFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
        public final static String TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT = "ManageListDashboardFragment";
        private boolean mIsPersonal = true;
        private boolean mIsShared = true;

        private boolean mShouldbeon;
        private boolean mInitialShouldbeon;
        private boolean mShouldbeon1;
        private boolean mInitialShouldbeon1;
        protected Button mPreferencesDoneButton;
        final boolean isPersonal = true;
        final boolean isShared = true;
        private ListsFragment mListsFragment;
        ToggleButton one; 
        ToggleButton two;
        public static ManageListDashboardFragment newInstance(final FragmentManager manager, final int animation) {
            final ManageListDashboardFragment fragment = new ManageListDashboardFragment();
            final Bundle arguments = new Bundle();
            arguments.putInt(ANIMATION, animation);
            fragment.setArguments(arguments);
            final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.manage_news_categories_container);
            fragmentInfo.setFragmentTag(TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT);
            FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
            return fragment;
        }

        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.fragment_manage_lists, container, false);
            final Bundle arguments = getArguments();
            final int animation = arguments.getInt(ANIMATION, 0);
            final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();
            if (animation != 0) {
                activity.animateTitle(R.id.actionbar_title, arguments.getInt(ANIMATION, 0));
            }
            return view;
        }
        protected void setupClickListeners() {
            mIsPersonal = isPersonal;
            mPreferencesDoneButton = (Button) getActivity().findViewById(R.id.button_done);
            Typeface face = Typeface.createFromAsset(mPreferencesDoneButton.getContext().getAssets(),
                    "fonts/proxima-nova-regular.ttf");
            mPreferencesDoneButton.setTypeface(face);
            mPreferencesDoneButton.setOnClickListener(this);
            mPreferencesDoneButton.setEnabled(((ManageListDashboardActivity) getActivity()).isDoneButtonEnabled());
        }
        @Override
        public void onActivityCreated(final Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title);
            titleView.setText(R.string.manage_dashboard_lists);

            initManageListDashboardFragment();

        }

        private void initManageListDashboardFragment() {
            populateData();
            setupClickListeners();

            Button personalbutton = (Button) getActivity().findViewById(R.id.button_personal_list);
            personalbutton.setOnClickListener(this);
            Button sharedbutton = (Button) getActivity().findViewById(R.id.button_shared_list);
            sharedbutton.setOnClickListener(this);
            one = (ToggleButton) getView().findViewById(R.id.personal_list_toggle_control);
            one.setOnCheckedChangeListener(this);
            two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
            two.setOnCheckedChangeListener(this);


        }
        protected void populateData() {
            SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false);

        }


        @Override
        public void onClick(final View view) {
            final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();

            switch (view.getId()) {
                case R.id.button_personal_list:
                    mIsPersonal = !mIsPersonal;
                    mIsShared = false;
                    one.setChecked(mIsPersonal);
                    return;

                case R.id.button_shared_list:
                    mIsPersonal = false;
                    mIsShared = !mIsShared;
                    two.setChecked(mIsShared);
                    return;


                case R.id.button_done:
                    saveUserPreferences();

                    DashboardFragment.getInstance().getListsFragment().setIsPersonal(mIsPersonal);

                    break;

                default:
                    break;
            }

            activity.onBackPressed();
        }
        private void saveUserPreferences() {

            final SharedPreferencesManager manager = SharedPreferencesManager.getInstance();
            if (manager.shouldManageListDashboard() == mIsPersonal) {
                manager.shouldManageListDashboard();
            }


        }


        protected void toggleDoneButton() {
            boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
            boolean isDoneEnabled = (
                     mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                    || isUserPreferencesUpdated);
            mPreferencesDoneButton.setEnabled(isDoneEnabled);
        }
        public void onUpdate() {
            mListsFragment.onUpdate();
        }

        @Override
        public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {

            if(isChecked){
                 if (buttonView == one) {
                        two.setChecked(false);
                    }
                 else if (buttonView == two) {
                        one.setChecked(false);

                    }
                }
            switch (buttonView.getId()) {
                case R.id.personal_list_toggle_control:

                    mShouldbeon = isChecked;
                    if(mShouldbeon = isChecked && mShouldbeon1 != isChecked){


                    }
                    break;
                case R.id.shared_list_toggle_control:

                    mShouldbeon1 = isChecked;
                    if(mShouldbeon1 = isChecked && mShouldbeon != isChecked){
                        }
                    break;

                default:
                    break;
            }
            toggleDoneButton();
        }

    }
È stato utile?

Soluzione

I would recommend you to create a 2 static variables in your ManageListDashboardFragment to save the current state of your togglebuttons. Update those variables when done button is pressed. And in your initManageListDashboardFragment() update the state of your toggle button according to current values of static variable. Of course, the static variable value would be preserved only as long as your app is running. If you want your states to be preserved even on different instances of your app then use SharedPreference instead of static variables.

Example of SharedPreferance:-

public class ManageListDashboardFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
    public final static String TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT = "ManageListDashboardFragment";
    private boolean mIsPersonal = true;
    private boolean mIsShared = true;
    private SharedPreferences sp;
    private boolean mShouldbeon;
    private boolean mInitialShouldbeon;
    private boolean mShouldbeon1;
    private boolean mInitialShouldbeon1;
    protected Button mPreferencesDoneButton;
    final boolean isPersonal = true;
    final boolean isShared = true;
    private ListsFragment mListsFragment;
    ToggleButton one; 
    ToggleButton two;
    public static ManageListDashboardFragment newInstance(final FragmentManager manager, final int animation) {
        final ManageListDashboardFragment fragment = new ManageListDashboardFragment();
        final Bundle arguments = new Bundle();
        arguments.putInt(ANIMATION, animation);
        fragment.setArguments(arguments);
        final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.manage_news_categories_container);
        fragmentInfo.setFragmentTag(TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT);
        FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_manage_lists, container, false);
        final Bundle arguments = getArguments();
        final int animation = arguments.getInt(ANIMATION, 0);
        final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();
        if (animation != 0) {
            activity.animateTitle(R.id.actionbar_title, arguments.getInt(ANIMATION, 0));
        }
        return view;
    }
    protected void setupClickListeners() {
        mIsPersonal = isPersonal;
        mPreferencesDoneButton = (Button) getActivity().findViewById(R.id.button_done);
        Typeface face = Typeface.createFromAsset(mPreferencesDoneButton.getContext().getAssets(),
                "fonts/proxima-nova-regular.ttf");
        mPreferencesDoneButton.setTypeface(face);
        mPreferencesDoneButton.setOnClickListener(this);
        mPreferencesDoneButton.setEnabled(((ManageListDashboardActivity) getActivity()).isDoneButtonEnabled());
    }
    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title);
        titleView.setText(R.string.manage_dashboard_lists);

        initManageListDashboardFragment();

    }
    Button sharedbutton;
    Button personalbutton;
    private void initManageListDashboardFragment() {
        populateData();
        setupClickListeners();
        sp = PreferenceManager.getDefaultSharedPreferences(getActivity());


        personalbutton = (Button) getActivity().findViewById(R.id.button_personal_list);
        personalbutton.setOnClickListener(this);
        sharedbutton = (Button) getActivity().findViewById(R.id.button_shared_list);
        sharedbutton.setOnClickListener(this);
        one = (ToggleButton) getView().findViewById(R.id.personal_list_toggle_control);
        one.setOnCheckedChangeListener(this);
        boolean toogle = sp.getBoolean("toggle", true);
        two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control);
        two.setOnCheckedChangeListener(this);
        if(toogle) {    
        one.setChecked(true);
        }
        else {
        two.setChecked(true);
        }

    }
    protected void populateData() {
        SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false);

    }


    @Override
    public void onClick(final View view) {
        final ManageListDashboardActivity activity = (ManageListDashboardActivity) getActivity();

        switch (view.getId()) {
            case R.id.button_personal_list:
                mIsPersonal = !mIsPersonal;
                mIsShared = false;
                one.setChecked(mIsPersonal);

                sp.edit().putBoolean("toggle", mIsPersonal).commit();
                return;

            case R.id.button_shared_list:
                mIsPersonal = false;
                mIsShared = !mIsShared;
                two.setChecked(mIsShared);
                sp.edit().putBoolean("toggle", !mIsShared).commit();
                return;


            case R.id.button_done:
                saveUserPreferences();

                DashboardFragment.getInstance().getListsFragment().setIsPersonal(mIsPersonal);

                break;

            default:
                break;
        }

        activity.onBackPressed();
    }
    private void saveUserPreferences() {

        final SharedPreferencesManager manager = SharedPreferencesManager.getInstance();
        if (manager.shouldManageListDashboard() == mIsPersonal) {
            manager.shouldManageListDashboard();
        }


    }


    protected void toggleDoneButton() {
        boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated();
        boolean isDoneEnabled = (
                 mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1
                || isUserPreferencesUpdated);
        mPreferencesDoneButton.setEnabled(isDoneEnabled);
    }
    public void onUpdate() {
        mListsFragment.onUpdate();
    }

    @Override
    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {

        if(isChecked){
             if (buttonView == one) {
                    two.setChecked(false);
                    personalbutton.setEnabled(false);
                    sharedbutton.setEnabled(true);
                    one.setEnabled(false);
                    two.setEnabled(true);
                    sp.edit().putBoolean("toggle", true).commit();
                }
             else if (buttonView == two) {
                    one.setChecked(false);
                    personalbutton.setEnabled(true);
                    sharedbutton.setEnabled(false);
                    two.setEnabled(false);
                    one.setEnabled(true);
                    sp.edit().putBoolean("toggle", false).commit();
                }
            }
        switch (buttonView.getId()) {
            case R.id.personal_list_toggle_control:

                mShouldbeon = isChecked;
                if(mShouldbeon = isChecked && mShouldbeon1 != isChecked){


                }
                break;
            case R.id.shared_list_toggle_control:

                mShouldbeon1 = isChecked;
                if(mShouldbeon1 = isChecked && mShouldbeon != isChecked){
                    }
                break;

            default:
                break;
        }
        toggleDoneButton();
    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top