Open same fragment after activity recreate

1.9k Views Asked by At

I have fragment container activity in which I replace fragments as per operations. In one fragment I am providing options to change primary color for app. So user can change toolbar and other components tint color. but after applying theme i recreate activity so my toolbar in base activity also reinitialized with new color code.

Now my problem is my app sequence is like this Setting > User settings > Theme Settings

so when user change theme i want to display same fragment again after recreate. I have seen other questions also and applied logic also but not working for me.

Code to change theme

private void setThemePreference(String primary, String primaryDark, String text) {
    int requiredLength = 0;

    if (primary.length() < 6) {
        requiredLength = 6 - primary.length();

        for (int i = 0; i < requiredLength; i++) {

            primary = "0" + primary;
        }
    }

    if (primaryDark.length() < 6) {
        requiredLength = 6 - primaryDark.length();

        for (int i = 0; i < requiredLength; i++) {

            primaryDark = "0" + primaryDark;
        }
    }

    if (text.length() < 6) {
        requiredLength = 6 - text.length();

        for (int i = 0; i < requiredLength; i++) {

            text = "0" + text;
        }
    }

    UserPreferenceManager.preferencePutString(
        AppConstants.SharedPreferenceKeys.PrimaryColor,primary);
    UserPreferenceManager.preferencePutString(
        AppConstants.SharedPreferenceKeys.PrimaryDarkColor, primaryDark);
    UserPreferenceManager.preferencePutString(
        AppConstants.SharedPreferenceKeys.PrimaryTextColor, text);
    UserPreferenceManager.preferencePutBoolean(
        AppConstants.SharedPreferenceKeys.IS_ThemeChanged, true);

    TaskStackBuilder.create(getActivity())
        .addNextIntent(new Intent(getActivity(), MainActivity.class))
        .addNextIntent(getActivity().getIntent())
        .startActivities();

    // applied this also
    //getActivity().recreate();

    getActivity().overridePendingTransition(0, 0);
}

Code to open theme fragment

getActivity().getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
    .addToBackStack(TAG)
    .replace(R.id.container, fragment)
    .commit();

Please guys help. I have not much experience and not even fresh new developer.

1

There are 1 best solutions below

11
On

Try this, it may help as you have stored is_theme changed,

now use Intent to restart the activity

startActivity(new Intent(MainActivity.this,MainActivity.class));

When activity starts in its onResume method check whether theme is changed or not, and if it was changed then call the fragment

@Override
    protected void onResume() {
        super.onResume();

        if(UserPreferenceManager.preferencePutBoolean(
                AppConstants.SharedPreferenceKeys.IS_ThemeChanged, true)){
            UserPreferenceManager.preferencePutBoolean(
                AppConstants.SharedPreferenceKeys.IS_ThemeChanged, false)
            YourActivity activity = (YourActivity) getActivity();
            activity.goToSecondFragment();
        }
    }

The method

public void goToSameFragment(){
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment sameFragment = new sameFragment();
    ft.replace(R.id.fragment_container, sameFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(null);
    ft.commit();
}