如何从一个活动刷新所有活动

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

How to refresh all activity from one activity

问题

我有多个带有各自活动的片段。

当我在MainActivity中进行刷新时,我希望所有片段的活动也能刷新。因为我在与数据库一起工作,这涉及到刷新其他片段以更新数据。

MainActivity.kt

swipeRefresh.setOnRefreshListener {
      // 仅刷新MainActivity
      // 我不知道如何在此处调用片段的活动

      swipeRefresh.isRefreshing = false
}
英文:

I have multiple fragments with its activity.

When I refresh in MainActivity, I want all fragment's activity to refresh as well. Because I work with database and it involves refreshing others fragments to update the data.

MainActivity.kt

swipeRefresh.setOnRefreshListener {
      // refreshing MainActivity only
      // I don't know how to call fragment's activity here

      swipeRefresh.isRefreshing = false
}

答案1

得分: 0

片段(Fragment)依赖于活动(Activity),而活动(Activity)不依赖于片段(Fragment)。您在活动中所做的操作会影响到片段(Fragment)。以此作为起点进行查找。

英文:

Fragment is dependent on activity and activity is not dependent on fragment. What you do in activity will affect the fragment. Do use this a starting point to look up.

答案2

得分: -1

在活动中,您可以调用 recreate() 方法来重新创建活动 (API 11+),或者您可以在活动内部使用以下方法来刷新活动。

finish();
startActivity(getIntent());
英文:

In the activity, you can call recreate() to "recreate" the activity (API 11+)

or you can use this for refreshing an Activity from within itself.

finish();
startActivity(getIntent());

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

发表评论

匿名网友

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

确定