Last active
August 29, 2015 14:08
-
-
Save theSociableme/402d07c5c29aa6d2471d to your computer and use it in GitHub Desktop.
removing nested fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Activity - Back button | |
@Override | |
public boolean dispatchKeyEvent(KeyEvent event) { | |
int keyCode = event.getKeyCode(); | |
if ((keyCode == KeyEvent.KEYCODE_BACK)) { | |
if (event.getAction() == KeyEvent.ACTION_UP) { | |
if (menuDrawer.isDrawerOpen(Gravity.START)) { | |
menuDrawer.closeDrawers(); | |
} else { | |
closePage(null, null); | |
if(mRootPageFragment.isDetached()){ | |
finish(); | |
} | |
} | |
} | |
return true; | |
} | |
return super.dispatchKeyEvent(event); | |
} | |
public void closePage(PageFragment closePage, String closeTo) { | |
mRootPageFragment.closeLastPage(closePage, closeTo); | |
} | |
// Drawer which is the mRootPageFragment | |
@Override | |
public String closeLastPage(PageFragment closePage, String closeTo) { | |
PageFragment parentPage = null; | |
if(mFragDeck.size() > 0) { | |
PageFragment lastPage = mFragDeck.getLast(); | |
if (!StringUtils.isEmpty(closeTo)) { | |
Iterator<PageFragment> it = mFragDeck.descendingIterator(); | |
while (it.hasNext()) { | |
PageFragment candidate = it.next(); | |
if (StringUtils.equals(closeTo, candidate.getPageName())) { | |
parentPage = candidate; | |
break; | |
} | |
} | |
} | |
if (closePage == null || closePage == lastPage) { | |
if(lastPage instanceof LigerNavigatorFragment){ | |
lastPage.closeLastPage(closePage, closeTo); | |
}else { | |
FragmentTransaction ft = mContext.getSupportFragmentManager().beginTransaction(); | |
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); | |
mFragDeck.removeLast(); | |
lastPage.doPageClosed(); | |
ft.remove(lastPage); | |
if (parentPage == null) { | |
if (mFragDeck.size() > 0) { | |
parentPage = mFragDeck.getLast(); | |
} | |
} else { | |
popTo(ft, parentPage); | |
} | |
String parentUpdateArgs = lastPage.getParentUpdateArgs(); | |
if (parentPage != null) { | |
parentPage.setChildArgs(parentUpdateArgs); | |
parentPage.doPageAppear(); | |
((DefaultMainActivity) getActivity()).setActionBarTitle(parentPage.getPageTitle()); | |
} | |
ft.commit(); | |
} | |
logStack("closeLastPage"); | |
} else { | |
lastPage.closeLastPage(closePage, closeTo); | |
} | |
} | |
if(mFragDeck.size() == 0 || (mFragDeck.size() == 1 && !mFragDeck.getLast().isDetached())){ | |
FragmentTransaction ft = mContext.getSupportFragmentManager().beginTransaction(); | |
ft.remove(this); | |
ft.commit(); | |
} | |
return parentPage == null ? null : parentPage.getPageName(); | |
} | |
// Navigator Which is contained in the drawer frag deck | |
@Override | |
public String closeLastPage(PageFragment closePage, String closeTo) { | |
PageFragment parentPage = null; | |
if(mFragDeck.size() > 0) { | |
PageFragment lastPage = mFragDeck.getLast(); | |
if (!StringUtils.isEmpty(closeTo)) { | |
Iterator<PageFragment> it = mFragDeck.descendingIterator(); | |
while (it.hasNext()) { | |
PageFragment candidate = it.next(); | |
if (StringUtils.equals(closeTo, candidate.getPageName())) { | |
parentPage = candidate; | |
break; | |
} | |
} | |
} | |
if(closePage == null || closePage == lastPage) { | |
FragmentManager fm = getFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); | |
mFragDeck.removeLast(); | |
lastPage.doPageClosed(); | |
ft.remove(lastPage); | |
if (parentPage == null) { | |
if (mFragDeck.size() > 0) { | |
parentPage = mFragDeck.getLast(); | |
} | |
} else { | |
popTo(ft, parentPage); | |
} | |
String parentUpdateArgs = lastPage.getParentUpdateArgs(); | |
if (parentPage != null) { | |
parentPage.setChildArgs(parentUpdateArgs); | |
parentPage.doPageAppear(); | |
((DefaultMainActivity) getActivity()).setActionBarTitle(parentPage.getPageTitle()); | |
} | |
ft.commit(); | |
logStack("closeLastPage"); | |
} | |
} | |
if(mFragDeck.size() == 0){ | |
FragmentTransaction ft = mContext.getSupportFragmentManager().beginTransaction(); | |
ft.remove(this); | |
ft.commit(); | |
} | |
return parentPage == null ? null : parentPage.getPageName(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment