alStateException如果我需要添加onNewInte

alStateException如果我需要添加onNewInte

本文介绍了如何避免一个IllegalStateException如果我需要添加onNewIntent(一片段)或运行时更改后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚的一个IllegalStateException是什么,为什么会发生,当你试图经过实例状态已保存到提交FragmentTransactions。我已经通过大部分关于这一主题的流行问题#1阅读以及的。

我在其中,而应用程序在等待我创建了一个片段,显示自定义进度微调特定方案对一些搜索结果进来,所以我的搜索活动是singleTop模式下运行,并且依赖于 onNewIntent()来执行搜索和显示的微调。这一切工作正常,但是当运行时更改进来(如方向变化)失败。如果我试图改变方向之后,除去微调片段的进展,或者语音搜索后加入微调时,我会得到一个IllegalStateException。我的设置是这样的:

  @覆盖
保护无效onNewIntent(意向意图){
    如果(intent.getAction()。等于(Intent.ACTION_SEARCH)){
        performSearch(intent.getStringExtra(SearchManager.QUERY));
    }
}

performSearch()方法将请求发送到我的服务器,然后添加进度微调片段像这样...

 私人无效showProgressSpinner(){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction交易= fragmentManager.beginTransaction();
    transaction.setCustomAnimations(R.anim.fragment_fade_in,R.anim.fragment_fade_out);
    ProgressFragment微调= ProgressFragment.newInstance();
    transaction.add(R.id.searchContainer,微调,微调);
    器transaction.commit();
}

然后,当搜索结果通过异步回调进来,我删除进度微调片段像这样...

 私人无效dismissProgressSpinner(){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction交易= fragmentManager.beginTransaction();
    transaction.setCustomAnimations(R.anim.fragment_fade_in,R.anim.fragment_fade_out);
    ProgressFragment微调=(ProgressFragment)fragmentManager.findFragmentByTag(微调);
    如果(微调!= NULL){
        transaction.remove(微调);
    }
    器transaction.commit();
}

如果改变方向时进来在显示微调片段的进展,我得到的IllegalStateException异常时,搜索结果中返回,我尝试删除微调片段。据,把FragmentTransaction在 onPostResume()能有所帮助,因为你都保证届时恢复状态。这确实是工作,但我需要从搜索结果中删除片段在我的异步回调,不能当活动恢复。

我的问题是,我怎么能犯的状态改变之后,但在我的异步回调此片段的交易?我已经尝试过使用 commitAllowingStateLoss(),但我仍然得到例外,因为我的微调片段仍引用旧被毁活动。什么是处理这个问题的最佳方式?


解决方案

your fragment was recreated after config change, ie. after user have rotated screen - your spinner fragment will be destroyed and recreated, and after onAttach it will reference new activity instance. Also all of this process is done by android on UI thread in single message, so there is no chance that your async operation callback (which should execute also on UI thread) gets executed in the middle.

I assume here you are not creating some local reference to activity inside ProgressFragment.

You could write additional logic that would make sure your commit is called in valid moment. ie. in your activity onStart set some static boolean allowCommit to true, and in onPause set it to false. Also add some static variable, searchWasFinished, and in your async callback check if allowCommit is true if so then immediately remove spinner, if not then only set searchWasFinished to true. Inside your Activity.onStart check if searchWasFinished==true and if so then remove fragment with commit. This is just an idea, probably more logic would have to be put in it.

这篇关于如何避免一个IllegalStateException如果我需要添加onNewIntent(一片段)或运行时更改后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:28