从父 Activity 重新创建 Fragment。

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

Recreate Fragment from parent Activity

问题

我需要重新创建一个来自其父 Activity 的片段。我找到的解决方案都是 10 年前的,而且是用 Java 写的。有没有一种简洁的方法,不需要太多样板代码来实现类似这样的功能?

例如从 Activity 中:

getFragments().recreate()

到目前为止,我尝试了不同的解决方案,但都无法使其正常工作。有没有一种方法可以做到这一点?

英文:

I need to recreate a fragment from its parent Activity. The solutions I found were all 10 years old and in Java. Is there a neat way without too much boiler plate to implement something like this?

Like from Activity:

getFragments().recreate()

I have tried different solutions so far but have not been able to make it work. Is there a way to do this?

答案1

得分: 0

我需要从其父活动重新创建一个片段。

只需用新实例替换它:

fragmentManager.commit { replace(idOfFragmentToReplace, MyFragment.create(...)) }

或者,你可以重新创建整个活动。

英文:

> I need to recreate a fragment from its parent Activity.

Just replace it with a new instance:

fragmentManager.commit { replace(idOfFragmentToReplace, MyFragment.create(...)) }

Or you could recreate the whole Activity.

huangapple
  • 本文由 发表于 2023年4月20日 03:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058347.html
匿名

发表评论

匿名网友

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

确定