Skip to content

How to recreate/refresh a FragmentSlide #105

@CeJienAJPC

Description

@CeJienAJPC

I have 2 FragmentSlides. In the first slide, the user defines the app language. In the second slide, the user defines day/night mode. Both the information will be saved using SharedPreferences.

Now I want to recreate/refresh/update the current fragment with user-defined preferences. To elaborate, in 1st slide the default language is English. The user defines the app language to German. Likewise in the 2nd slide, the default mode is day mode. The user defines the default to night mode. I need to recreate the fragment, in order to display the contents of the Fragment in German in night mode.

I am not sure on how to recreate the Fragments. I tried the below code.

public class FragmentChooseMode extends SlideFragment {

    @OnCheckedChanged(R.id.frchmo_switch_mode)
    public void chooseMode(boolean isChecked) {
        if (isChecked) {
            sharedPreferences.edit().putBoolean(KeyApplyMode, true).apply();
            getActivity().recreate();
        } else {
            sharedPreferences.edit().putBoolean(KeyApplyMode, false).apply();
            getActivity().recreate();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        sharedPreferencesSettings();
        final View view = inflater.inflate(R.layout.fragment_choose_mode, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

(I knew) it hasn't worked as expected. getActivity().recreate() recreates an activity. this means the activity will be displaying both the fragments right from the beginning (which I don't want).

If the user was in the second fragment, then on recreate/refresh, I want only the second fragment to be displayed to the user. How can this be done?

Regards

Metadata

Metadata

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions