2

I have a MainActivity, that is supposed to switch between two fragments: Content and Settings (extends PreferenceFragmentCompat). Everything worked fine, but recently I implemented Dagger 2 dependency injection, and my Settings fragment started to blink. When you press settings item on the bottom navigation bar, sometimes settings fragment appears, then disappears for few milliseconds and appears again.

I don't see any explainable reason why this happens, and what exactly happens.

Can you, please, explain what's happening and how to fix this?

Here is the code of my MainActivity.

MainActivity.java

public class MainActivity extends BaseActivity
    implements BottomBarConstructor.BottomBarSelectItemListener,
    MainActivityContract.View {

public final static int REQUEST_CODE = 1;

@Inject
MainActivityContract.Presenter<MainActivityContract.View> presenter;

private EffectsControlFragment effectsControlFragment;
private SettingsFragment settingsFragment;

private FragmentManager fragmentManager;

private BottomNavigationView bottomNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    createFragments();
    bindViews();
    initializeFragment();

    getActivityComponent().inject(this);
    presenter.onAttach(this);
    presenter.onBottomBarCreate();
}

@Override
public void onSelectItem(String fragmentId) {
    changeFragment(fragmentId);
}


@Override
public void createBottomBar(List<BottomBarItem> items) {
    new BottomBarConstructor(this, bottomNavigationView).create(items);
}

public void changeFragment(String fragmentId) {
    switch (fragmentId) {
        case "main":
            replaceFragment(effectsControlFragment, fragmentId);
            break;
        case "settings":
            replaceFragment(settingsFragment, fragmentId);
            break;
        default:
            replaceFragment(EffectsControlFragment.newInstance(), fragmentId);
    }
}

private void replaceFragment(Fragment fragment, String fragmentId) {
    fragmentManager.beginTransaction()
            .replace(R.id.fragment_container, fragment, fragmentId)
            .commit();
}

private void createFragments() {
    effectsControlFragment = EffectsControlFragment.newInstance();
    settingsFragment = SettingsFragment.newInstance();
    fragmentManager = getSupportFragmentManager();
}

private void bindViews() {
    bottomNavigationView = findViewById(R.id.navigation_bar);
}

private void initializeFragment() {
    fragmentManager.beginTransaction()
            .add(R.id.fragment_container, EffectsControlFragment.newInstance())
            .commit();

}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32

3 Answers3

1

Adding .addToBackStack(null) to the fragmentTransaction solved my problem.

private void replaceFragment(Fragment fragment, String fragmentId) {
        fragmentManager.beginTransaction()
                .replace(R.id.fragment_container, fragment, fragmentId)
                .addToBackStack(null)
                .commit();
    }
Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32
  • how odd; this also fixed blinking for me... but then the tabs don't behave correctly when back button pressed – pete Aug 11 '20 at 04:19
0

After so many hours of experiment found a better solution. I may be late, but sure it will be help for someone.

All you need to do is just remove the fragmentTransition from your code. fragmentTransaction!!.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)

This will remove your transition animation and replace the contents inside the fragment, also removes blink effect.

Happy Coding, Thank you

user7418129
  • 1,074
  • 14
  • 18
-1

solved by making transaction manager do commitNow() instead of commit()

pete
  • 1,878
  • 2
  • 23
  • 43