Reverse for internal fragments

Maybe the title of the question is common, but that is not the problem. I use one action in which the frame layout I replace the fragment FragmentTransactionand FragmentManager. this is done successfully, I got a new fragment, say, fragment A. Now in fragment A I have viewpagerthree tabs, and each tab has fragment, so in fragment A I have fragment B, fragment C and fragment D.

The entire Activity-> script replacing framelayout → Fragment A-> three tabs (three fragments of fragment B, fragment C and fragment D)

Now the Problem :

When I am on the second tab, if I return, I will immediately proceed to activity. I want to do if in the second or third tab go to the previous tabs, and if in the first tab go to the activity page.

Maybe the backtrace fragment is automatically maintaned, but if it is used only in Activity, then only. right now i'm using snippet to hold tabs.

What I tried:

I created a listener:

PageChangeListener:

public interface PageChangeListener {
    public void onNextRequest();
    public void onPrivousRequest();
}

This is implemented in fragment A:

 @Override
    public void onNextRequest() {
        if(mViewPagerPage + 1 < mPageCount)
        {
            viewPager.setCurrentItem(++mViewPagerPage,true);
        }
    }

    @Override
    public void onPrivousRequest() {

        if(mViewPagerPage -1 >= 0)
        {
            viewPager.setCurrentItem(--mViewPagerPage,true);
        }

    }

But in this task there is: where to use the onPrivousRequest () method, since fragments do not have the onBackPressed method.

Any help would be appreciated.

+4
source share
3 answers

where to use the onPrivousRequest () method, since fragments do not have the onBackPressed method.

:

1:

public interface OnFragmentBackPressed {
    boolean onBackPressed();
}

2.

public class TabFragment extends Fragment implements OnFragmentBackPressed {

   @Override
   public boolean onBackPressed() {
       if (Check if you have to move on other tabs) {
            return true; 
        } else {
            return false;
        }
    }
}

3: onBackPressed()

public class FragmentContainerActivity extends AppCompatActivity {

    @Override 
    public void onBackPressed() {
    Fragment mF = getSupportFragmentManager().findFragmentById(R.id.main_container);
       if (!(mF instanceof OnFragmentBackPressed) || !((OnFragmentBackPressed) mF).onBackPressed()) {
          super.onBackPressed(); // This won't get called unless you return false in Fragment onBackPressed();
       }
    }
}
+3
@Override
public void onBackPressed() {
  if(i_dont_want_to_leave) {
     myViewPager.setCurrentItem(0, true);
  } else {
    super.onBackPressed(); // This will pop the Activity from the stack.
  }
}
+2

, , , .

public interface IOnBackPressed {
   void onBack();
}

IOnBackPressed onBackPressedImpl

( FragmentA),

 FragmentA fragA = new FragmentA();
    onBackPressedImpl = (IOnBackPressed) fragA ;
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.container, fragA, FragmentA.class.getSimpleName());
    ft.commit();

onBackPressed()

 @Override
public void onBackPressed() {
   // super.onBackPressed(); IMPORTANT PART
    onBackPressedImpl.onBackPressed();
}

FragmentA OnBackPressed , .. -

@Override
public void onBack() {
    if (viewPager.getCurrentItem() != 0) {
        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1,false);
    }else{
         finish(); // OR somthing other that finish() depends your application backstack. 
    }

}

, , FragmentA, .

+2
source

Source: https://habr.com/ru/post/1696090/


All Articles