Skip to content

Instantly share code, notes, and snippets.

package com.thredup.android.fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
@mnisbet13
mnisbet13 / replace.java
Last active February 18, 2016 20:33
replacing nested fragments
public void replaceFragment(DialogFragment replacementFragment, String tag, String pageTitle) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_left_from_right, android.R.anim.fade_out, android.R.anim.fade_in,
R.anim.slide_out_right_from_left);
transaction.replace(R.id.fragment_content, replacementFragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
if (!TextUtils.isEmpty(pageTitle)) {
mToolbar.setTitle(pageTitle);
mToolbar.setNavigationIcon(R.drawable.ic_nav_back_green_selector);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<include layout="@layout/default_toolbar" />
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
if (getDialog() != null) {
getDialog().setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
onBackPressed();
}
return true;
}
});
public void onBackPressed() {
if (getChildFragmentManager().popBackStackImmediate()) {
BaseDialogFragment bdf = (BaseDialogFragment) getChildFragmentManager().getFragments()
.get(getChildFragmentManager().getFragments().size() — 1);
String title = getString(R.string.account_info);
if (bdf == null && getChildFragmentManager().findFragmentByTag(getString(R.string.order_history)) != null && getChildFragmentManager().findFragmentByTag(getString(R.string.order_history)).isVisible()) {
title = getString(R.string.order_history);
}
if (bdf != null) {
title = getNewTitle(bdf.getTitle());