Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the fragment exists

I am trying to talk to the fragments from activity.

Here in my MainActivity I am adding multiple fragments ok so for fine.

My main requirement is I don't want to add if fragment is already added.

So how can we check this condition?

Please help me some one.

code:-

 private void intializingFragments(Fragment fragment) {

        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    private View.OnClickListener intialization() {

        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int getId = v.getId();

                if (getId == R.id.first) {
                    intializingFragments(new Fragment1());

                } else if (getId == R.id.second) {
                    intializingFragments(new Fragment2());

                } else if (getId == R.id.third) {
                    intializingFragments(new Fragment3());
                } 
            }
        };
    }
like image 624
Krish Avatar asked Jun 01 '16 06:06

Krish


People also ask

How do I know if a fragment is created?

You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.

Can a fragment exist without activity?

Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.

How do you know if a sentence is fragment?

Use the following tests to determine whether or not your sentence is a sentence fragment. Find the verb. “Bob ran down the road.”. The verb is the action. In this case, “ran” is the verb. If the sentence has a verb, then your sentence has passed the first test. Locate the subject.

How to check if last added fragment is same as current fragment?

Alternatively, you can use your own variable to check whether the last added fragment is the same as the current fragment. Here is the sample code for it: Show activity on this post. You can go for popBackStack Pop the last fragment transition from the manager's fragment back stack.

How do I test a dialog fragment?

Though dialog fragments have UI elements, their layout is populated in a separate window, rather than in the activity itself. For this reason, use FragmentScenario.launch () to test dialog fragments. // Assumes that "MyDialogFragment" extends the DialogFragment class.

How to check if a fragment is null or not?

Use findFragmentById () or findFragmentByTag () to get a reference and check if its null, if not, check the reference's isAdded () or isVisible (). Thanks for contributing an answer to Stack Overflow!


6 Answers

You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.

Fragment fragmentA = fragmentManager.findFragmentByTag("frag1");
if (fragmentA == null) {
  // not exist
} else {
  // fragment exist
}

for example:- http://wiki.workassis.com/android-load-two-fragments-in-one-framelayout/

like image 143
Bikesh M Avatar answered Oct 17 '22 23:10

Bikesh M


You may find fragment in fragmentmanager:

List<Fragment> frags = getSupportFragmentManager().getFragments();
for (Fragment f : frags) { 
  <find what you want>... 
}

Or you may add fragment with tag:

getSupportFragmentManager()
  .beginTransaction()
  .add(R.id.frame, new MyFragment(), "frag1")
  .commit();

And find by tag

getSupportFragmentManager().findFragmentByTag("frag1");
like image 43
Kenumir Avatar answered Oct 17 '22 23:10

Kenumir


FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction transaction = fragmentManager.beginTransaction();
                Fragment topFragment = fragmentManager.findFragmentById(R.id.container);
                FragmentA fragmentA = new FragmentA();
                if(topFragment!= null)
                {
                    transaction.remove(topFragment);
                    transaction.add(R.id.container, fragmentA, "FA");
                    transaction.commit();
                }
                else
                {
                    transaction.add(R.id.container, fragmentA, "FA");
                    transaction.commit();
                }

try this

private void intializingFragments(Fragment fragment) {
      FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment topFragment = fragmentManager.findFragmentById(R.id.container);
        if(topFragment!= null)
        {
            fragmentTransaction.remove(topFragment);
            fragmentTransaction.add(R.id.fragment_container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
        else
        {
            fragmentTransaction.add(R.id.fragment_container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }

    }
like image 24
Naveen Tamrakar Avatar answered Oct 17 '22 22:10

Naveen Tamrakar


How to check whether a fragment exists, without knowing its resource ID or tag:

// MyActivity.kt
val exists: Boolean = supportFragmentManager
    .fragments
    .filterIsInstance<MyFragment>()
    .isNotEmpty()
like image 20
Tatsuya Fujisaki Avatar answered Oct 17 '22 22:10

Tatsuya Fujisaki


There are many ways by which you can track the last added fragment. The simplest one is by finding the tag within fragment manager. Here is the sample code of it:

public boolean isFragmentPresent(String tag) {
    Fragment frag = getSupportFragmentManager().findFragmentByTag(tag);
    if (frag instanceof HomeFragment) {
        return true;
    } else
        return false;
}

Alternatively, you can use your own variable to check whether the last added fragment is the same as the current fragment. Here is the sample code for it:

public boolean isCurrentFragment(Fragment fragment) {
    if (fragment instanceof HomeFragment && getLastAddedFragment() instanceof HomeFragment) { // getLastAddedFragment() is a method which return the last added fragment instance
        return true;
    } else
        return false;
}

And you can use it like:

if (isCurrentFragment(new HomeFragment())) {
    // Last added Fragment is the HomeFragment
}
like image 25
Yasir Tahir Avatar answered Oct 17 '22 22:10

Yasir Tahir


You can go for popBackStack Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned. enter link description here

like image 38
and-lab Avatar answered Oct 17 '22 23:10

and-lab