Himani
Himani

Reputation: 459

is there any way to find fragment id

I want to use fragment's adapter to activity. and I'm trying to access this

 HomeFragment fragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.);

but I'm using this method to replace the fragments to the one frame.

 public void replaceFragment(Fragment fragment, boolean isMainPage, Bundle bundle) {
    String backStateName = fragment.getClass().getName();
    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (bundle != null) {
        fragment.setArguments(bundle);
    }

    System.out.println("isMainPage :: " + isMainPage);
    System.out.println("FRAGMENT NAME :: " + backStateName + " fragmentPopped :: " + fragmentPopped);

    if (!fragmentPopped) { //fragment not in back stack, create it.
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.main_fragment, fragment);

        if (isMainPage) {
            getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        }
        int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
        System.out.println("backStackCount :: " + backStackCount);
        transaction.addToBackStack(backStateName);
       /* if (!backStateName.contains("HomeFragment")) {
            transaction.addToBackStack(backStateName);
        }*/


        transaction.commit();
    }
}

Upvotes: 0

Views: 903

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200020

You used

transaction.replace(R.id.main_fragment, fragment);

Therefore your Fragment is found at the id R.id.main_fragment

HomeFragment fragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.main_fragment);

Upvotes: 1

Related Questions