4

May be question title is common but the problem is not. I am using one Activity in which frame layout i am replacing the fragment with FragmentTransaction and FragmentManager . this is done successfully i got new fragment let say Fragment A. Now in Fragment A i have viewpager with three tabs and each tab have fragment so in Fragment A i have Fragment B,Fragment C and Fragment D.

Whole scenario Activity-> replacing framelayout -> Fragment A-> three tabs (three fragments Fragment B,Fragment C and Fragment D)

Now the Problem :

When i am in second tab if i back press then i move to activity directly. I want to make if in second or third tab then move to previous tabs and if in first tab then move to activity page.

May be in fragment backtrace is automatically maintaned but if used in Activity then only. right now i am using fragment to hold the tabs.

What i tried :

I have created listener :

PageChangeListener :

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

Implemented this 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 Problem is : where to use onPrivousRequest() method as fragments don't have onBackPressed Method.

Any help will be appreciated.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Annie
  • 2,397
  • 3
  • 18
  • 26
  • I can direct you to an answer but first, I need to know if you are comfortable with communication between activity and nested fragments via interfaces? – Somesh Kumar Apr 12 '18 at 05:57
  • @SomeshKumar yes i can use interfaces.. post the answer i will try and let you know – Annie Apr 12 '18 at 05:59

3 Answers3

2
@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.
  }
}
Nik
  • 1,991
  • 2
  • 13
  • 30
  • I have used tabs inside fragment so i can't override onBackPressed in fragment.. thank for your answer – Annie Apr 12 '18 at 05:54
  • from activity you can fire interface to fragment while you don't want to leave page – Nik Apr 12 '18 at 05:55
  • Activity just holds framelayout everything else is in fragment :( so interface is implemented in fragment – Annie Apr 12 '18 at 05:57
  • You can create interface with method and from when back pressed you can fire interface which implements in fragment where your viewpager is available. – Nik Apr 12 '18 at 06:03
  • that is what my question is in question you can see i have create interface implemeted in fragment.. now the question is where will be onBack press and where to use this onPrevious interface method.. – Annie Apr 12 '18 at 06:04
  • Onback press method will be in Activity – Nik Apr 12 '18 at 06:06
  • Not able to access method of child fragment from activity – Annie Apr 12 '18 at 06:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168788/discussion-between-nik-and-annie). – Nik Apr 12 '18 at 06:17
2

where to use onPrivousRequest() method as fragments don't have onBackPressed Method.

There is way to do so:

Step1: Create and interface

public interface OnFragmentBackPressed {
    boolean onBackPressed();
}

Step 2: Implement it in your Fragment

public class TabFragment extends Fragment implements OnFragmentBackPressed {

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

Step 3: In your Activity override 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's onBackPressed();
       }
    }
}
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
2

First of all, you need an interface that can communicate with your fragment whenever the back key is pressed.

public interface IOnBackPressed {
   void onBack();
}

Create object of IOnBackPressed in your activty lets call it onBackPressedImpl

Now you need to bind that with your fragment (in your case FragmentA) when you add it to activity

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

override onBackPressed() in your activty

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

and then in your FragmentA you need to implement OnBackPressed interface and implement its method and inside that method, you need to put the logic of changing tabs.. something like this

@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. 
    }

}

You need to handle the activity finish logic or the logic if your activity host different fragment other than FragmentA which is pretty easy.

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49