问题描述
这个问题是类似于我原来的问题,但我觉得有一个更好的办法的办法解决。
This question was similar to my original question, but I think there is a better way to approach a solution.
getIntent返回时setIntent后跟一个旋转的错意图
基本上,我的主要活动
,它扩展了 FragmentActivity
,有两种情况,其中一个片段我通过一个Intent这一活动。
Basically, in my main Activity
, which extends FragmentActivity
, there are two instances where in a Fragment I pass an Intent to this Activity.
这是有问题的code:
This is the code that is having the problem:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
if (getIntent() != null && getIntent().getData() != null) {
String data = getIntent().getDataString();
//setIntent(null);
的 setIntent
是要解决我的问题的尝试,但未能奏效。
The setIntent
was an attempt to solve my problem but it failed to work.
会发生什么事是,当我点击可以追溯到我的活动作为一个Intent一个链接,然后我去下一个片段,然后转动手机,这是通过两个片段前显示出来的时候,我再次致电意向 getIntent
。
What happens is when I click on a link that goes back to my Activity as an Intent, then I go to the next Fragment, then I rotate the phone, the Intent that was passed two Fragments ago shows up again when I call getIntent
.
所以,当我得到的数据出来的意向,我怎么能设置这么 getIntent
的下一次调用将返回null?或者,它是有意义的只是在意向中的数据为空呢?
So, when I get the data out of the Intent, how can I set it so the next call to getIntent
will return null? Or, would it make sense to just have the data in the Intent be null instead?
我试图避免在共享preferences更新信息来处理这个问题。
I am trying to avoid updating information in the SharedPreferences to handle this.
推荐答案
我发现,似乎工作的解决方案,但我希望有一个更好的方式来做到这一点。
I found a solution that appears to work, but I expect there is a better way to do it.
if (getIntent() != null && getIntent().getData() != null) {
String data = getIntent().getDataString();
getIntent().setData(null);
setIntent(null);
我觉得 setIntent
是没有任何用处,但使用setData(空)
似乎做的招。
I think the setIntent
isn't of any use, but the setData(null)
seems to do the trick.
这篇关于如何获得getIntent()来调用一个Intent集活动后返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!