Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to recreate/refresh a FragmentSlide #105

Closed
CeJienAJPC opened this issue Jul 30, 2016 · 4 comments
Closed

How to recreate/refresh a FragmentSlide #105

CeJienAJPC opened this issue Jul 30, 2016 · 4 comments
Assignees
Milestone

Comments

@CeJienAJPC
Copy link

CeJienAJPC commented Jul 30, 2016

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

@heinrichreimer
Copy link
Owner

The simplest solution would be to either remove and re-add the specific fragment or much simpler just call notifyDataSetChanged() on your IntroActivity. That will refresh the pager adapter and the whole intro UI so don't call that too often.

@Lackmake
Copy link

Lackmake commented Aug 4, 2016

Actually getActivity().recreate() worked for me when the user chose a different theme. Until release 1.5.6. The position was even the same after the recreation so that is no problem. But with release 1.5.7, calling recreate() on the IntroActivity just ends the activity(or something else but i end up in the activity that called the intro).
Your solution with calling notifyDataSetChanged() also does not work for me as the new theme is not applied.
I think the behavior with 1.5.6 and earlier would be the correct one. You might want to look into this again.

@heinrichreimer
Copy link
Owner

So why do you change the theme in the intro? Not really a usecase I can imagine...
For usability it would probably be better to just not modify the intro theme at all and apply the theme after the intro has finished.

Also changing the language is not very common so that should probably not be in the intro. Remember the intro is there to show your users the most important features of your app or to let them make settings the app can't work without. A better user experience would be to set the app language to the devices language per default and letting users change that on a separate settings page. I guess most of the users want their apps to have the same language as their device anyway, so why should you interrupt them with such an obvious question.

Both the night mode option and the language preference are things only very few of overall users will want to change. Both of them belong in the settings activity.

To address this issue technical, when you call notifyDataSetChanged() the activity will stay alive but will update the SlideAdapter which will update the view hierachy of all slides. As the app theme can not be modified without recreating the activity the slides that are updated get their resources from the same theme that was there before notifyDataSetChanged().
Please keep in mind that recreating an activity is a very memory intensive operation that you should use very rarely. I don`t see what would legitimate that effort for just changing an option.
If and only if you really need to include that obvious options in your activity intro, you should be absolutely fine with a simple switch and a text view indicating the selected state.

@Lackmake
Copy link

In my case the user can select an app theme and instantly see what it looks like. Yeah of course i could apply the theme after the intro, but then the user would know how it looks and maybe has to go into the settings to change it back.
The interesting part is that it worked right for me until release 1.5.6., so maybe its just a bug? I am using recreate() also in my preferenceActivity where it also works and also keeps the scroll position etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants