Skip to content Skip to sidebar Skip to footer

Why Is Getfragmentmanager().findfragmentbytag(string.valueof(some Integer)) Not Working?

I have a stack of Fragments and trying go back in the stack using getFragmentManager().findFragmentByTag() method. This method does return the object if called this way: getFragmen

Solution 1:

how do you set your fragmentTag? something like this would be necessary:

ft.add(fragmentContainerId, mfragment, String.valueOf(++currentPositionInTheStack));

Solution 2:

Have figured out how to navigate forward and back in the stack:

for the forward navigation , if next fragment already exists , used hide() and show() methods:

Buttonbutton= (Button)findViewById(R.id.new_fragment);
        button.setOnClickListener(newOnClickListener() {
            publicvoidonClick(View v) {
                CountingFragmentcurrentFragment= (CountingFragment)getFragmentManager().findFragmentByTag(String.valueOf(currentPositionInTheStack));
                intnextLevel= currentPositionInTheStack+1;
                currentPositionInTheStack++;
                CountingFragmentnextFragment= (CountingFragment)getFragmentManager().findFragmentByTag(String.valueOf(currentPositionInTheStack));
                if (nextFragment == null) {
                    addFragmentToStack();

                }
                else
                {
                    FragmentTransactionfragmentTransaction= getFragmentManager().beginTransaction();
                    fragmentTransaction.hide(currentFragment);
                    fragmentTransaction.show(nextFragment);
                    fragmentTransaction.commit();

                }

            }
        });

For the back navigation , had to override onBackPressed method:

publicvoidonBackPressed() {
            super.onBackPressed();  
            currentPositionInTheStack--;
            Log.d("Current Position In The Stack", String.valueOf(currentPositionInTheStack));
            if (currentPositionInTheStack ==0) {
                //do smth
            } }

The Fragment in the original question was NULL and should be null, though, why it returned the Object , if I did getFragmentManager().findFragmentByTag("3"), for example, is not clear.

Post a Comment for "Why Is Getfragmentmanager().findfragmentbytag(string.valueof(some Integer)) Not Working?"