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.