0

I have an activity with a Framelayout and a BottomNavigationView...I have 4 fragments (A,B,C,D)...The thing is when I switch from A to B after clicking on the menu item to load fragment B, fragment A gets destroyed...I added a Log message on all the callback methods (OnAttach, OnCreate, OnCreateView.....etc) involved in Fragment lifecycle and onDestroyView is ALWAYS called when I change fragments...So when I come back to a previously opened fragment, onCreateView gets called again..

Here's my activity class:

    public class Home extends AppCompatActivity
{
    private BottomNavigationView.OnNavigationItemSelectedListener 
mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item)
    {
        Fragment fragment = null;
        Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        switch (item.getItemId())
        {
            case R.id.navigation_a:
                if (!(currentFragment instanceof FragmentA))
                    fragment = FragmentA.newInstance();
                break;
            case R.id.navigation_b:
                if (!(currentFragment instanceof FragmentB))
                    fragment = FragmentB.newInstance();
                break;
            case R.id.navigation_c:
                if (!(currentFragment instanceof FragmentC))
                    fragment = FragmentC.newInstance();
                break;
            case R.id.navigation_d:
                if (!(currentFragment instanceof FragmentD))
                    fragment = FragmentD.newInstance();
                break;
        }

        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
            return true;
        }

        return false;
    }

};


//TODO Handle life-cycle methods when switching between fragments
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction()
            .add(R.id.fragment_container, FragementA.newInstance())
            .commit();
    fm.popBackStack();

    BottomNavigationView navigation = findViewById(R.id.navigation);        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        moveTaskToBack(true);
        return true;
        }

    return false;
    }

}

I'd like to know what am I missing here actually....Thanks in advance

Claude Hangui
  • 426
  • 3
  • 8
  • 29
  • @Claude...Have you figured it out? – GvSharma Oct 13 '17 at 08:46
  • @GvSharma....I'm truly sorry for my late response mate !!... Actually I used a viewpager inside..I didn't have the time to test ideas proposed below as I was in a haste...But I do think that adding the fragment to the backstack definitely fixes the issue.... – Claude Hangui Nov 24 '17 at 11:04

3 Answers3

2

well your not adding the fragment to the backstack.

sdfbhg
  • 109
  • 5
1

You are creating new instances every click.

Do retain the reference to those if you want to replace with the same one OR add then to backstack of the fragmentransaction, but you will need tags for navigating trough the previous ones.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
-1

Try to use setRetainInstance(true); for your fragments.

// this method is only called once for this fragment
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // retain this fragment
    setRetainInstance(true);
}
John Le
  • 1,116
  • 9
  • 12