Switching Fragements without loading the whole screen

Asked

Viewed 77 times

1

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

And since data comes from the web, it takes time and precious resources, wanted a way that as long as the app was open the already covered screens would not be recharged.

on my onNavigationItemSelected which is on Mainactivity the transition code for fragements is as follows:

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 only prevents 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, as follows Main activity

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

the result of any of these Fragments would result in a new Activity, would be to show the details, and this Activity would have 3 more Fragments.

  • 1

    How are you implementing your main screen? Why, depending on your application, it will take a check to update the layout. Explain more about your application, please.

  • @lucas_marciano added an explanation at the end

  • 1

    Danilo, in your Ragment you are loading the data in onResumo or onCreate?

  • Everything on onCreate, recommends me a better way?

  • 1

    Friend your problem is all in the life cycle of Fragment, I point you to see that video. From what I understand of your problem, you load all your layout in onCreat(), and it is indicated to use onResume to avoid loading Fragment without need, I also saw that you use . add instead of replace, watch this video and one studied on documentation

  • Thanks, with the video I was able to make my Fragment not be destroyed when rotating the device, but it did not keep the fragement when the fragement change was made, but with the code below I arrived at the solution. Thanks, because I also wanted to keep my Fragment in the rotation too

Show 1 more comment

1 answer

1


Able 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 he checks if the last fragmente is not null, if it is not null instead of destroying it only concealed, so for next time he uses the show instead of the conditional add above If anyone has a better solution than this, please share

Browser other questions tagged

You are not signed in. Login or sign up in order to post.