I have 3 fragments open on condition of First fragments list item Click >> Second fragment open and so on Second. But when I click on first fragment's blank space it automatically open second fragment and click on second fragment's blank space it goes to third fragment
This is class I have maintain for fragment transaction
public static void addABCDHomeFragment(FragmentManager fragManager, String categoryName) {
FragmentTransaction ft = fragManager.beginTransaction();
Fragment abcdHomeFragment = fragManager.findFragmentByTag(ABCD_HOME_FRAGMENT);
if (abcdHomeFragment == null || abcdHomeFragment.isRemoving()) {
ABCDHomeFragment homeFragment = new ABCDHomeFragment(categoryName);
ft.add(R.id.frame_layout, homeFragment, ABCD_HOME_FRAGMENT);
ft.addToBackStack(ABCD_HOME_FRAGMENT);
ft.commit();
} else if (abcdHomeFragment != null && abcdHomeFragment.isAdded()) {
fragManager.popBackStack();
ABCDHomeFragment homeFragment = new ABCDHomeFragment(categoryName);
ft.replace(R.id.frame_layout, homeFragment, ABCD_HOME_FRAGMENT);
ft.addToBackStack(ABCD_HOME_FRAGMENT);
ft.commit();
}
}
public static void addProductListFragment(FragmentManager fragmentManager,String categoryId ,
String parent , int option) {
FragmentTransaction ftproductlist = fragmentManager.beginTransaction();
Fragment plistFragment = fragmentManager.findFragmentByTag(PRODUCT_LIST_FRAGMENT);
ProductListFragment productListFragment = new ProductListFragment(categoryId,parent,option);
if (plistFragment != null && plistFragment.isAdded() && plistFragment.isVisible()) {
fragmentManager.popBackStack();
ftproductlist.add(R.id.frame_layout, productListFragment,PRODUCT_LIST_FRAGMENT);
ftproductlist.addToBackStack(PRODUCT_LIST_FRAGMENT);
ftproductlist.commit();
} else {
ftproductlist.replace(R.id.frame_layout, productListFragment, PRODUCT_LIST_FRAGMENT);
ftproductlist.addToBackStack(PRODUCT_LIST_FRAGMENT);
ftproductlist.commit();
}
}
public static void removeProductListFragment(FragmentManager fragmentManager) {
FragmentTransaction ftproductlist = fragmentManager.beginTransaction();
Fragment productListFragment = fragmentManager.findFragmentByTag(PRODUCT_LIST_FRAGMENT);
if (productListFragment != null && productListFragment.isAdded()) {
ftproductlist.remove(productListFragment);
fragmentManager.popBackStack();
ftproductlist.commit();
}
}
public static void addProductDetailFragment(FragmentManager fragManager,String productId,String parent, String productName) {
FragmentTransaction ftproduct = fragManager.beginTransaction();
Fragment productDetailFragment = fragManager.findFragmentByTag(PRODUCT_DETAIL_FRAGMENT);
if (productDetailFragment == null || productDetailFragment.isRemoving()) {
ProductDetailFragment dtailFragment = new ProductDetailFragment(productId, parent,productName);
ftproduct.add(R.id.frame_layout, dtailFragment, PRODUCT_DETAIL_FRAGMENT);
ftproduct.addToBackStack(PRODUCT_DETAIL_FRAGMENT);
ftproduct.commit();
} else if (productDetailFragment != null && productDetailFragment.isAdded()) {
fragManager.popBackStack();
ProductDetailFragment dtailFragment = new ProductDetailFragment(productId,parent,productName);
ftproduct.replace(R.id.frame_layout, dtailFragment,PRODUCT_DETAIL_FRAGMENT);
ftproduct.addToBackStack(PRODUCT_DETAIL_FRAGMENT);
ftproduct.commit();
}
}
Related
I am using bottom navigation drawer to switch between fragments, the problem is that every time I switch back to a fragment it gets recreated.
How can I save the state of fragment and resume it when switched back to it?
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
#Override
public void onTabSelected(int position) {
if (position==0){
loadFragment(new Daily());
}
if (position==1){
loadFragment(new Trending());
}
if (position==2){
loadFragment(new Random());
}
}
#Override
public void onTabUnselected(int position) {
}
#Override
public void onTabReselected(int position) {
}
});
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.attach( fragment);
transaction.addToBackStack(null);
transaction.commit();
}
The issue is that you are always creating a new Fragment on any onTabSelected. So in order to fix it, you need to work with your FragmentManager.
Possible solution: use the add and show/hide methods.
Example:
private static final String DAILY_TAG = BuildConfig.APPLICATION_ID + ".DAILY_TAG";
private static final String TRENDING_TAG = BuildConfig.APPLICATION_ID + ".TRENDING_TAG";
private static final String RANDOM_TAG = BuildConfig.APPLICATION_ID + ".RANDOM_TAG";
public void onTabSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
hideFragment(TRENDING_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(DAILY_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Daily(), DAILY_TAG)
}
transaction.commitNow()
} else if (position == 1) {
hideFragment(DAILY_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(TRENDING_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Trending(), TRENDING_TAG)
}
transaction.commitNow()
} else {
hideFragment(TRENDING_TAG)
hideFragment(DAILY_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(RANDOM_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Random(), RANDOM_TAG)
}
transaction.commitNow()
}
fragments.put(position, fragment);
loadFragment(fragment);
}
private void hideFragment(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager()
Fragment currentFragment = fragmentManager.findFragmentByTag(tag)
if (currentFragment != null) {
fragmentManager.beginTransaction().hide(currentFragment).commitNow()
}
}
PS - The code can be optimized.
How can I bring fragment to top of other fragments ?
I added fragment like bellow :
#OnClick(R.id.imbEvents)
void clickEvents() {
String tagName = returnStatusFragment(getString(R.string.events_fragment));
if (!TextUtils.isEmpty(tagName) && tagName.equals(getString(R.string.events_fragment))) {
//Fragment is opened
} else {
if (getActivity() != null) {
frameWorkTableFragment.setVisibility(View.VISIBLE);
EventsFragment eventsFragment = new EventsFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
eventsFragment,
getString(R.string.events_fragment))
.addToBackStack(getString(R.string.events_fragment))
.commit();
}
}
}
I used from bellow code to bring fragment to top but close all last fragments:
getActivity().getSupportFragmentManager().popBackStack(tagName, 0);
Method of returnStatusFragment is :
private String returnStatusFragment(String tagName) {
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment tempFragment = fragmentManager.findFragmentByTag(tagName);
if (tempFragment != null) {
return tempFragment.getTag();
} else {
return "";
}
}
My doBackTabZero:
#Override
public void doBackTabZero(String nameFragment) {
android.support.v4.app.FragmentManager fragmentManagers = getActivity().getSupportFragmentManager();
Log.i("ASDSADASDSASD", "3 " + nameFragment + " " + fragmentManagers.getBackStackEntryCount());
if (fragmentManagers.getBackStackEntryCount() > 1) {
fragmentManagers.popBackStackImmediate();
} else {
if (nameFragment.equals("EventsFragment")) {
BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
EventBus.getDefault().post(busDisplayStatusFrameLayout);
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentManager.popBackStackImmediate();
fragmentTransaction.remove(EventsFragment.this);
fragmentTransaction.commit();
}
}
}
replace this
getActivity().getSupportFragmentManager().popBackStack(tagName, 0);
with
Fragment eventsFragment = fragmentManager.findFragmentByTag(getString(R.string.events_fragment));
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
eventsFragment,
getString(R.string.events_fragment)).commit();//don't add to back stack again
onBackPressed
#Override
public void doBackTabZero(String nameFragment) {
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
if(fragmentManager.getBackStackEntryCount() > 1 ){
fragmentManager.popBackStackImmediate();
}else{
super.onBackPressed();
}
}
I have a problem of SuportMap fragment, while it's displaying a blank fragment when I switch back to the Map fragment, and this is the situation:
I have Main activity with left drawer, the drawer contains tow items to display fragment A and fragment B, Fragment A containing tab host with 2 tabs (map tab and second Tab).
the application is displaying the Map on lunching, and it's ok, but if I select the second item in the drawer to go to the fragment B, and back to fragment A, the Map fragment is displaying a blank fragment, and if I select another tab and back to the map tab then it displaying the map again successfully. and I do not know what is the wrong.
MainActivity:
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
MainFragment mainFragment = (MainFragment) getSupportFragmentManager().findFragmentByTag(MainFragment.TAG);
if (id == R.id.nav_home && mainFragment == null) {
showFragment(new MapFragment(), "MapFragment");
} else if (id == R.id.nav_gallery &&
getSupportFragmentManager().findFragmentByTag(GalleryFragment.TAG) == null) {
showFragment(new B(), "B");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void showFragment(Fragment fragment, String tag) {
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(tag, 0);
if (!fragmentPopped) { //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, fragment);
ft.addToBackStack(tag);
ft.commit();
}
}
Fragment A:
private void initTabHost() {
mTHost.setOnTabChangedListener(this);
mTHost.setup();
TabHost.TabSpec MapSpec = mTHost.newTabSpec(PeopleFragment.TAG);
MapSpec.setIndicator(getTabIndicator(R.string.tool_bar_people, R.drawable.selector_map_tab));
MapSpec.setContent(new DummyTabContent(getActivity()));
mTHost.addTab(MapSpec);
TabHost.TabSpec secondSpec = mTHost.newTabSpec(SecondFragment.TAG);
secondSpec.setIndicator(getTabIndicator(R.string.tool_bar_news, R.drawable.selector_second_tab));
secondSpec.setContent(new DummyTabContent(getActivity()));
mTHost.addTab(secondSpec);
}
private View getTabIndicator(int title, int icon) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_tab_layout, null);
ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
ivIcon.setImageResource(icon);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setText(title);
return view;
}
#Override
public void onTabChanged(String tabId) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentByTag(MapFragment.TAG);
SecondFragment secondFragment = (SecondFragment) fragmentManager.findFragmentByTag(SecondFragment.TAG);
if (mapFragment != null) {
fragmentTransaction.detach(mapFragment);
}
if (secondFragment != null) {
fragmentTransaction.detach(secondFragment);
}
if (tabId.equalsIgnoreCase(MapFragment.TAG)) {
if (mapFragment == null) {
fragmentTransaction.add(R.id.fl_real_tab_content, new MapFragment(), MapFragment.TAG);
} else {
fragmentTransaction.attach(mapFragment);
}
} else if (tabId.equalsIgnoreCase(SecondFragment.TAG)) {
if (secondFragment == null) {
fragmentTransaction.add(R.id.fl_real_tab_content, new SecondFragment(), SecondFragment.TAG);
} else {
fragmentTransaction.attach(SecondFragment);
}
}
fragmentTransaction.commit();
}
MapFragment:
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
public void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the mMap.
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this);
}
Any help what Should I do and what is the problem?
I have fixed this issue in a work around solution, through add android:launchMode=="SingleInstance" to the activity in the manifests file.
in this case when we go back to the activity, then the activity will not create new fragments and reset the states from first.
I am using a navigation drawer.So when my application starts i am calling the homeFragment and keeping it in the backstack. Now if the user selects any options from the navigation drawer i am opening the respective fragment but without adding them to backstack. So what i want is that even when user has open 10 fragment, on pressing back they should be taken back to homeFragment only. But with my code the app exits on pressing back even homeFragment is in the backstack.
Code to openFragments
public static void replaceFragment(FragmentActivity activity, Fragment fragment, boolean addToBackStack) {
try {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = activity.getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(fragmentTag, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.frag_container, fragment, fragmentTag);
if (addToBackStack) {
ft.addToBackStack(backStateName);
}
ft.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
From MainActivity
private void init() {
setUpToolBar();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFrag = (NavigationDrawerFrag) getSupportFragmentManager().findFragmentById(R.id.frag_nav_drawer);
drawerFrag.setUp(drawerLayout, toolbar, R.id.frag_nav_drawer);
CommonFunctions.replaceFragment(this, new HomeFrag(), true);
}
On NavigationDrawer Item click
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
CommonFunctions.replaceFragment(getActivity(), new ProfileFrag(), false);
} else if (position == 2) {
CommonFunctions.replaceFragment(getActivity(), new CelebrityFrag(), false);
} else if (position == 4) {
CommonFunctions.replaceFragment(getActivity(), new AboutUsFrag(), false);
} else if (position == 5) {
CommonFunctions.replaceFragment(getActivity(), new TermsAndConditions(), false);
}
lvNavItems.setItemChecked(position, true);
//lvNavItems.setSelection(position);
lvNavItems.setSelected(true);
mDrawerLayout.closeDrawer(fragContainer);
}
Add onBackPressed() method in activity to check backstack and if entry is more then zero so call popbackstack method.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
//Put this in your onNavigationItemSelected of MainActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//FrameLayout where you load your home fragment tools:layout="#layout/frag_home"
ft.replace(R.id.content_frame,nav_selected);
if(getSupportFragmentManager().getBackStackEntryCount() != 0){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
}
//Don't add Home screen
if(id != R.id.nav_home){
ft.addToBackStack(null);
}
ft.commit();
Since the TabActivity is deprecated I tried to replace it with Fragments which has been already mentioned in developer android website. But as you guys already know there was an issue about replacing tabs with fragments, since there will be only one activity ,which is Fragment Activity, there is no back stack for each of the fragments and as you can see in the other SO questions most of the developers were saying that you need to manage your own custom back stack as a solution.
I have created and implemented my own custom back stack as you can see below, it works perfect I can track each fragment in each tab.
Tab1
Fragment1 > Fragment2 > Fragment3
Tab2
Fragment5 > Fragment6
According to navigation above, if I navigate from Fragment1 through Fragment 3 and After that If I change the tab to Tab2 to and come back to Tab1, I can still see the Fragment3 and I can even navigate back to Fragment1 with my custom back stack.
But the issue is, When I come back to Tab1 and see the Fragment3, the Fragment3 is re-created and I can not see the changes as I left behind before I change the tab to Tab2.
Here is my Custom back stack;
public static HashMap<String, Stack<Fragment>> customBackStack;
public static Stack<Fragment> simpleStack;
public static Stack<Fragment> contactStack;
public static Stack<Fragment> customStack;
public static Stack<Fragment> throttleStack;
public static Stack<Fragment> homeStack;
customBackStack = new HashMap<String, Stack<Fragment>>();
homeStack = new Stack<Fragment>();
simpleStack = new Stack<Fragment>();
contactStack = new Stack<Fragment>();
customStack = new Stack<Fragment>();
throttleStack = new Stack<Fragment>();
customBackStack.put("home", homeStack);
customBackStack.put("simple", simpleStack);
customBackStack.put("contacts", contactStack);
customBackStack.put("custom", customStack);
customBackStack.put("throttle", throttleStack);
And here is onTabChanged method;
public void onTabChanged(String tabId) {
TabInfo newTab = mTabs.get(tabId);
if (mLastTab != newTab) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
if (!customBackStack.get(tabId).isEmpty()) {
Fragment fragment = customBackStack.get(tabId).pop();
customBackStack.get(tabId).push(fragment);
ft.replace(mContainerId, fragment);
}
} else {
if (!customBackStack.get(tabId).isEmpty()) {
Fragment fragment = customBackStack.get(tabId).pop();
customBackStack.get(tabId).push(fragment);
ft.replace(mContainerId, fragment);
}
}
}
mLastTab = newTab;
ft.commit();
mActivity.getSupportFragmentManager().executePendingTransactions();
}
}
And here is onBackPressed;
public void onBackPressed() {
Stack<Fragment> stack = customBackStack.get(mTabHost.getCurrentTabTag());
if (stack.isEmpty()) {
super.onBackPressed();
} else {
Fragment fragment = stack.pop();
if (fragment.isVisible()) {
if (stack.isEmpty()) {
super.onBackPressed();
} else {
Fragment frg = stack.pop();
customBackStack.get(mTabHost.getCurrentTabTag()).push(frg);
transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right,
R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.realtabcontent, frg).commit();
}
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.realtabcontent, fragment).commit();
}
}
}
As a result, custom back stack works fine except the last fragment before changing the tab is getting re-created after coming back to the tab, its not resuming as like in tab activity.
Any idea how to solve it ?
EDIT
You can find my sample Application here as a solution regarding to this issue.
GitHub
I modified googles sample tablistener to following:
public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
public SherlockFragment mFragment;
private final Stack<SherlockFragment> mStack;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null, null);
}
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args, Stack<SherlockFragment> stack) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
mStack = stack;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// we need to reattach ALL fragments thats in the custom stack, if we don't we'll have problems with setCustomAnimation for fragments.
if (mFragment == null) {
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.replace(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
if(mStack != null) {
for(SherlockFragment fragment: mStack) {
ft.attach(fragment);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
if(mStack != null) {
for(SherlockFragment fragment: mStack) {
if(fragment!= null && !fragment.isDetached()) {
ft.detach(fragment);
}
}
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
And the activity onKeyDown methode I override to following:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
int index = getSupportActionBar().getSelectedNavigationIndex();
Stack<SherlockFragment> stack = null;
SherlockFragment fragment = null;
String rootTag = null;
switch(index) {
case 0:
stack = mWatersTabListener.mStack;
fragment = mWatersTabListener.mFragment;
rootTag = mWatersTabListener.mTag;
break;
case 1:
stack = mHarborTabListener.mStack;
fragment = mHarborTabListener.mFragment;
rootTag = mHarborTabListener.mTag;
break;
}
if(stack.isEmpty()) {
return super.onKeyDown(keyCode, event);
} else {
SherlockFragment topFragment = stack.pop();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
if(topFragment != null && !topFragment.isDetached()) {
ft.detach(topFragment);
}
if(stack.isEmpty()) {
ft.replace(android.R.id.content, fragment, rootTag);
ft.commit();
return true;
} else {
SherlockFragment nextFragment = stack.peek();
ft.replace(android.R.id.content, nextFragment);
ft.commit();
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
The important things to take note of is that you have to attach all fragments in your custom stack when selected and you have to detach all as well when unselected. If there is problem with understanding the code snippets just ask.