How to prevent new Fragment from being recreated again and again?


stern bafsar

Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new fragment ...how to stop it?

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

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
    tabLayout.addTab(tabLayout.newTab().setText("One"), true);
    tabLayout.addTab(tabLayout.newTab().setText("Two"), false);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {                                                      
            @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (tab.getPosition() == 0) {
                Fragment fragment = new Fragment1();

                chageFragment(fragment);
            } 

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

  }

private void chageFragment(Fragment fragment) {
     getFragmentManager().beginTransaction().add(R.id.container,    fragment,fragment.getClass().getSimpleName()).commit();
}
Kevin

Try changing this line replaceinstead add.

getFragmentManager().beginTransaction().add(R.id.container, fragment, fragment.getClass().getSimpleName()).commit();

should

getFragmentManager().beginTransaction().replace(R.id.container, fragment, fragment.getClass().getSimpleName()).commit();

Related


How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

How to prevent tabs from being recreated on every activity call

Raff I have an application with an index page that has tabs for navigation. The page has multiple subpages, which are implemented as fragments. Some of my child fragment views also have tabs in them for more navigation like below The problem I'm having is when

How to prevent tabs from being recreated on every activity call

Raff I have an application with an index page that has tabs for navigation. The page has multiple subpages, which are implemented as fragments. Some of my child fragment views also have tabs in them for more navigation like below The problem I'm having is when

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g