Search code examples
androidandroid-fragmentsfragmenttransactionfragmentmanager

How to Check that FragmentTransaction.replace() Updates Fragment


I am a little confused as to how you would check that FragmentTransaction replaces a Fragment. What I mean by this is that you make a reference to a Layout in your XML file when you call this method. The method does not actually replace the Layout with a Fragment, but replaces whats inside said Layout with the Fragment you want. Because of this, you cannot check to see if it is updated by referencing the Layout. In addition to this, you cannot define a Fragment inside the XML and reference it as it will be casted as a View and not a Fragment. How would you go about testing this method's execution? Thank You!


Solution

  • The question is a bit unclear to me, so I'll answer to the best of my abilities. If you're just asking how you can check if a certain line in a method has been executed, you can log it using https://developer.android.com/reference/android/util/Log.html.

    If you're looking to see if a certain fragment (let's say with a layout file called fragment1.xml) has been successfully placed in your layout, you can check using:

    Fragment myFragment = getSupportFragmentManager.findFragmentByID(R.id.fragment1); if (myFragment != null) // has not been put in your layout successfully.