Let us consider that i have 2 fragment a & b
Fragment a contains view pager, horizontal grid view and buttons images etc with scroll.
When i click a item in Fragment a
it goes to fragment b
and when i click on back button in fragment b
it comes back to fragment a
. Is all working perfect.
My issue is in back press of the fragment b
, it load the fragment a
from begin. I would like to show the exact place on fragment a
(say bottom) where the transaction begin.
I am doing all the fragment transaction in adapter of each item and code is like below.
Bundle arguments = new Bundle();
Fragment fragment = null;
MyClass cls= (MyClass ) obj.get(position);
arguments.putParcelable("single", offer);
// Start a new fragment
fragment = new DetailFragment();
fragment.setArguments(arguments);
FragmentTransaction transaction = activity .getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.replace(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID);
transaction.addToBackStack(DetailFragment.ARG_ITEM_ID);
transaction.commit();
How to load the fragment transaction begin position when came back from other fragment.
I understand your problem. You can use
transaction.add(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID);
instead of
transaction.replace(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID);
May be, If it not solve your problem then use following mechanism, it will solve your problem:-
Check if your Fragment is already in your stack then attach already existing fragment.
else add new instanse of Frament.
use sacenario like this code :
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the androidfragment if exists */
if(androidFragment!=null)
ft.detach(androidFragment);
/** Detaches the applefragment if exists */
if(appleFragment!=null)
ft.detach(appleFragment);
/** If current tab is android */
if(tabId.equalsIgnoreCase("android")){
if(androidFragment==null){
/** Create AndroidFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AndroidFragment(), "android");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(androidFragment);
}
}else{ /** If current tab is apple */
if(appleFragment==null){
/** Create AppleFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AppleFragment(), "apple");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(appleFragment);
}
}
ft.commit();
EDIT :
transaction.add will show the previous fragment on backpress and will not go through the fragment life cycle
For Reference see this onTabChanged : http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/