Android 6.0权限申请与DialogFragment的show()结合所遇到的

最近在开发项目时需要用到Android 6.0权限申请,解决的快捷方式是用Google提供的EasyPermissions

于是在这个EasyPermissions中,onRequestPermissionsResult() 等同于onResume()的生命周期,在调用DialogFragmentshow()方法时,就会报以下错误:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 

经过一遍搜索,问题在于

onSaveInstanceState()方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存完状态后再给它添加Fragment就会出错

解决方法就是把commit() 方法替换成 commitAllowingStateLoss()

api中对commitAllowingStateLoss()给出的说法是:

Like commit() but allows the commit to be executed after an activity’s state is saved.

而对于DialogFragmentshow()方法,源码是:

public void show(FragmentManager manager, String tag) {
    mDismissed = false;
    mShownByMe = true;
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commit();
}

因此,我写了个工具类的方法将commit()方法替换成 commitAllowingStateLoss()
如下:

/**
 * 弹出语音对话框
 *
 * @param manager
 * @param dialog
 */
public static void showDialog(FragmentManager manager, VoiceDialog dialog) {
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(dialog, "voice_dialog");
    ft.commitAllowingStateLoss();
}

当然,出现该异常可能还有其他原因,这是我所遇到的


4 条评论

昵称
  1. 匿名

    说的好我儿

  2. Sonia

    With any innovation, there is no reason to start with the adaptation that was common 5 years back.
    Start learning as to what is current. Around this authorship, HTML5 and CSS3 will be the criteria.
    Begin here and work the right path forward. You can expect to actually reach prevent a lot of the problems intrinsic
    in earlier incarnations.
    You need us to generate a site before I do know exactly who to actually construct it?
    Yes. This is one way we discovered. We setup WordPress and created
    an easy website. Don’t be concerned, there are plenty of sources around to help you try this.
    Once set up therefore the website is ready to go, dissect it
    piece by portion. Determine what goes in the header label.
    Learn about meta tags. View the page way to obtain the
    home web page and start in order to comprehend DIVs.
    Find out how the navigation structure works, etc. There’s
    no much better tutor than real exercise. If you have an actual web site to play
    with, it generates almost everything the more exciting.
    You will definitely fundamentally discover your own art and gain skills.

    To know much more about basic features of html and html tutorial for
    beginners, please go to all of our website cool html tricks for websites

    Then you would no be interested in mastering
    HTML and CSS if you were perhaps not already an enthusiastic web user.
    Continue using the web everyday. See all sorts of web sites.
    See their own page source and understand how they structure their internet
    site. Appreciate good design from poor concept.
    By learning HTML principles, you’ll publish material on line with back link point text and embedded images, audio or movie with easy
    code.
    For anyone who is a joint venture partner marketer, having primary HTML skills will assist you to create minor to reasonable modifications on the signal when you publish content to market something.
    Learning to do upgrade HTML is not very tough at all.

    HTML tags have become easy to understand because they are logical as
    there are much paperwork readily available both online and offline.
    Knowing the way you use a software device like Microsoft Word, you can learn the fundamentals of HTML, it
    really is that facile.
    As much as sources, you’ve many cheap and simple sources including many HTML tutorial sites.
    With these help web sites, HTML is fully explained and demonstrated to be able to discover all
    of the abilities and techniques need to create an online site.

    It’s completely sensible for somebody without any knowledge to
    master enough HTML to build a web site page in one single time.

  3. 匿名

    哈哈来瞎逛,终于有更新

  4. 匿名

    可以,加油