重新创建带有不同额外数据的活动

huangapple go评论77阅读模式
英文:

Recreate activity with different extras

问题

我需要重新创建Activity - 打开一个带有不同额外信息的新屏幕,当我尝试使用recreate()时,它工作得很完美,但出现了一个难看的黑屏。

 setIntent(intent);
 this.recreate();

当我尝试使用finish()startActivity(newIntent)时,没有黑屏,但在某种程度上是在onDestroy()之前调用了onCreate()

 finish();
 startActivity(newIntent);

这是我的newIntent

 Intent newIntent = new Intent(this, DeviceActivity.class);
 newIntent.putExtra(Consts.INTENT_ID, device.getID());
 newIntent.putExtra(Consts.INTENT_DEVICE_NAME, device.getID());

有什么想法吗?

英文:

I need to recreate Activity - open a new screen with different extras,
when I try to do that with recreate() it works perfectly,
but there is an ugly black screen.

 setIntent(intent);
 this.recreate();

When I try to finish() and startActivity(newIntent) there is no black screen but in some way onCreate() called before onDestroy()

 finish();
 startActivity(newIntent);

this is my newIntent:

 Intent newIntent = new Intent(this, DeviceActivity.class);
 newIntent.putExtra(Consts.INTENT_ID, device.getID());
 newIntent.putExtra(Consts.INTENT_DEVICE_NAME, device.getID());

Any ideas?

答案1

得分: 1

无论 onCreate() 被调用的频率如何,只需检查 savedInstanceState == null,以确定何时运行该方法内的代码。当首次运行时,savedInstanceState 总是为 null。如果可能会在 Activity 生命周期后期运行,将一些繁重的代码移出 onCreate() 方法也可能是有意义的。

英文:

It does not matter how often onCreate() is being called; just check for savedInstanceState == null, in order to determine when to run code within that method. When it runs for the first time, savedInstanceState will always be null. Moving some heavy code outside of method onCreate() might also make sense, in case it could run later on in the Activity life-cycle.

答案2

得分: 0

这里的一个解决方案是在调用startActivity()时不调用finish()。这将创建一个您的活动的新实例,基本上是一个新的屏幕,但使用相同的代码。

但这完全取决于您希望用户看到什么。我们应该从用户交互的角度来讨论这个问题,而不是从“活动”和“意图”的角度,因为这些是实现细节。

英文:

One solution here is to call startActivity() without calling finish(). This will create a new instance of your activity, basically a new screen but using the same code.

But this depends entirely on what you want the user to see. We should talk about this in terms of the user interaction, not in terms of "activities" and "intents" which are implementation details.

答案3

得分: 0

好的,问题是我在onCreate() & onDestroyed() 中连接/断开与某个管理器的连接时出现了问题。

onDestroyed() 是异步发生的,我们无法知道系统何时会到达它(即使我们调用了finish())。

所以在我将管理器的启动移动到onPause() & onResume() 后,它就能够顺利工作,并解决了我的问题。

感谢大家的帮助。

英文:

Ok, so the problem was that I had connecting/disconnecting to some manager that fired from onCreate() & onDestroyed().

onDestroyed() Happens asynchronously and we have no way of knowing when the system will reach it (Even we call finish()).

so after I moved this manager launching to onPause() & onResume() it works smoothly and it solve my problem.

thank you all for your help

huangapple
  • 本文由 发表于 2020年10月1日 04:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64145524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定