Created
August 13, 2016 09:01
try make a hidden method in DialogFragment's source become public accessable
This file contains hidden or 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
package com.meizu.flyme.wallet.widget; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentTransaction; | |
import java.lang.reflect.Field; | |
/** | |
* Created by linshen on 16-8-13. | |
* | |
* Taken from the Marshmallow source code, can be found in: | |
* https://android.googlesource.com/platform/frameworks/base/+/marshmallow-release/core/java/android/app/DialogFragment.java | |
* | |
* Some private fields is accessed by reflection. | |
*/ | |
public class DialogFragmentExt extends DialogFragment { | |
private static final Class clz = DialogFragment.class; | |
public void showAllowingStateLoss(FragmentManager manager, String tag) { | |
//mDismissed = false; | |
try { | |
Field dismissed = clz.getDeclaredField("mDismissed"); | |
dismissed.setAccessible(true); | |
dismissed.set(this, false); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
//mShownByMe = true; | |
try { | |
Field shown = clz.getDeclaredField("mShownByMe"); | |
shown.setAccessible(true); | |
shown.set(this, true); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
FragmentTransaction ft = manager.beginTransaction(); | |
ft.add(this, tag); | |
ft.commitAllowingStateLoss(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment