-1

Can someone explain why this code isn't working?

    Fragment f = fragmentManager.findFragmentByTag(tag);

    if (f == null) {
        fragmentManager.beginTransaction().replace(R.id.protokoll_content, fragment, tag).commit();
    } else {
        fragmentManager.beginTransaction().replace(R.id.protokoll_content, f).commit();
    }

I don't think you need much more to get what I'm trying to do, and I don't think the rest of the code is relevant to the problem. I'm like 99% certain this is the trouble zone.

I'm simply trying to load an existing version of a fragment by calling it by its tag.

In my mind the above code would execute as follows:

IF (the fragment has no existing instance) { create a new one with a unique tag and replace the existing fragments in the container with it }

OTHERWISE (the fragment DOES have an existing instance) { load that existing version instead of creating a new one and replace the current fragment with the found existing fragment }

Edit

Slightly updated code. Same problem, new fragments are made instead of existing instances of the fragments getting loaded.

Clarification

New fragments are made each time instead of existing ones getting loaded. If I write something, go to a new fragment and then try to go back the the old fragment all the text is wiped because new fragments get made instead of old ones getting loaded.

Simon Hyll
  • 3,265
  • 3
  • 24
  • 44
  • have you debugged for the value of tag?? – Vivek Mishra Jan 06 '16 at 06:01
  • Possible duplicate of [Android replace the current fragment with another fragment](http://stackoverflow.com/questions/8163104/android-replace-the-current-fragment-with-another-fragment) – Sharp Edge Jan 06 '16 at 06:02
  • 1
    your `if` condition says if fragment is null you are loading `null fragment`. what exactly you are doing. ? `fragmentManager.beginTransaction().replace(R.id.activity_content, fragment, tag).commit();` – Bharatesh Jan 06 '16 at 06:06
  • Bharat - thanks, stupid mistake... no sleep and coding does not compute... the original problem still persists, new fragments are made not existing instances getting loaded – Simon Hyll Jan 06 '16 at 17:58

1 Answers1

0

Below code is used to add fragment if no fragment is added to layout

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.add(R.id.fragment, fragment1, "fragment1");
fragmentTransaction.commit();

Above code will add Fragment1 to R.id.fragment

Below code is used to replace a fragment with another in a layout

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment, fragment2, "fragment2");
fragmentTransaction.commit();

Above code will replace a fragment with another in a layout

Ijas Ahamed N
  • 5,632
  • 5
  • 31
  • 53
  • Thanks but unfortunately the problem isnt that I cant create/add/replace fragments, it's that I dont want new instances of old fragments, I want to load previous instances. So when I edit the text in a fragment and then load another fragment, then try to go back to the old one all the text is wiped because it is a new fragment getting loaded. – Simon Hyll Jan 06 '16 at 17:56
  • Store edited text in sharedpreference before replacing.. When you reload previous fragment, take vales from sharepref and store back. Thats a way to restore fragment back to previous look – Ijas Ahamed N Jan 06 '16 at 17:59
  • Ljas Ahamed - but when adding to back stack you can simply press back button and all the text is still there, so the function exists to get it back without using a store text and recreate fragment option. :P – Simon Hyll Jan 06 '16 at 18:04