Change Fragos without loading the entire screen

1

I have a problem in my studies, when I change the Fragment and then I go back to the source Fragment the data is loaded all over again.

And as the data comes from the web, it takes precious time and resources, I wanted a way that while the app was open the screens already bundled were not reloaded.

in my onNavigationItemSelected that stays in MainActivity the code of transition of fragements is the following

if(mFragmentManager.findFragmentByTag("one") != null) {
                        //if the fragment exists, show it.
                        mFragmentManager.beginTransaction().show(mFragmentManager.findFragmentByTag("one")).commit();
                    } else {
                        //if the fragment does not exist, add it to fragment manager.
                        mFragmentManager.beginTransaction().add(R.id.content, new CategoriaFragment(), "one").commit();

}

but this is only preventing the screen from being reloaded when clicked on the menu that opens the current screen.

My main activity is in it that I call my fragments and control the transition between them.

public class MainActivity extends AppCompatActivity {
    private String tituloNavBar;
    private TextView mTextMessage;
    private CategoriaFragment mCategoriaFragment;
    private BuscaFragment mBuscaFragment;
    private FragmentManager mFragmentManager;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    tituloNavBar = getResources().getString(R.string.nav_bar_last);
                    titulo(tituloNavBar);
                    setFragment(mBuscaFragment,"Busca");
                    return true;
                case R.id.navigation_dashboard:
                    tituloNavBar = getResources().getString(R.string.nav_bar_start);
                    titulo(tituloNavBar);
                    if(mFragmentManager.findFragmentByTag("one") != null) {
                        //if the fragment exists, show it.
                        mFragmentManager.beginTransaction().show(mFragmentManager.findFragmentByTag("one")).commit();
                    } else {
                        //if the fragment does not exist, add it to fragment manager.
                        mFragmentManager.beginTransaction().add(R.id.content, new CategoriaFragment(), "one").commit();
                    }

                    return true;
                case R.id.navigation_notifications:
                    tituloNavBar = getResources().getString(R.string.nav_bar_filter);
                    titulo(tituloNavBar);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager = getSupportFragmentManager();
        tituloNavBar = getResources().getString(R.string.app_name);
        titulo(tituloNavBar);
        if(mBuscaFragment == null)
            mBuscaFragment = new BuscaFragment();


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

        //Fragmente padrão
        setFragment(mBuscaFragment,tituloNavBar);



    }


    //Substituido pela logica acima
    private void setFragment(Fragment fragment, String name){
        FragmentManager fm = getSupportFragmentManager();
        if (fm != null) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_left);
            ft.replace(R.id.content, fragment);
            ft.commit();
        }
    }
    private void titulo(String titulo){
        setTitle(titulo);

    }

}

is an activity that contains a menu and can call 3 fragments, this way Main activity

         - Fragment de busca
         - Fragment de categoria
         - Fragment de filtro

The result of any of these fragments would result in a new activity , it would be to show details, and this activity would have 3 fragments.

    
asked by anonymous 09.06.2017 / 06:46

1 answer

1

To solve my problem, in Activity I used this code to call Fragments

if(mFragmentManager.findFragmentByTag(BuscaFragment.KEY) != null) {
                        //if the fragment exists, show it.
                        mFragmentManager.beginTransaction().show(mFragmentManager.findFragmentByTag(ConcursoFragment.KEY)).commit();
                    } else {
                        //if the fragment does not exist, add it to fragment manager.
                        mFragmentManager.beginTransaction().add(R.id.content, new ConcursoFragment(), ConcursoFragment.KEY).commit();

                    }
                        if(mFragmentManager.findFragmentByTag(CategoriaFragment.KEY) != null){
                        //if the other fragment is visible, hide it.
                        mFragmentManager.beginTransaction().hide(mFragmentManager.findFragmentByTag(CategoriaFragment.KEY)).commit();
}

in logic

if(mFragmentManager.findFragmentByTag(CategoriaFragment.KEY) != null)

Here it checks if the last fragment is not null, if it is not null instead of destroy it only hides, so for next time it uses the show instead of the add in the above conditional if someone has a better solution than that, please share

    
10.06.2017 / 20:06