简体   繁体   English

片段未附加到活动,位于onResume中

[英]Fragment not attached to Activity, in onResume

I have a fragment which works fine basically. 我有一个片段,基本上可以正常工作。 In the onResume method I perform an api call to get fresh data when ever a the user gets to the activity/fragment. 在onResume方法中,当用户进入活动/片段时,我将执行api调用以获取新数据。

public void onResume(){
    super.onResume();

    //in pseudo
    do async call 
       success callback
          update ui

       failure callback
          update ui
}

This works fine most of the time. 在大多数情况下,这都能正常工作。 However I experience a problem when the user navigates back to the parent activity, before the update ui method has finished. 但是,当用户在更新ui方法完成之前导航回父活动时遇到一个问题。 The update ui just sets some text views. 更新ui只是设置一些文本视图。

Eg when the user navigates to the activity and the fragment the above onResume is called. 例如,当用户导航到活动和片段时,将调用上面的onResume。 Immediately the users goes back to the parent activity. 用户立即返回到父活动。 The async call and update ui isn't finished yet and I get: 异步调用和更新ui尚未完成,我得到:

FATAL EXCEPTION: main Process: com.project.foobar, PID: 4405 java.lang.IllegalStateException: Fragment MyDetailFragment{41ebb478} not attached to Activity 致命异常:主进程:com.project.foobar,PID:4405 java.lang.IllegalStateException:片段MyDetailFragment {41ebb478}未附加到Activity

This is thrown in the update ui method after success callback. 成功回调后,这将引发update ui方法。 I use square's retrofit to manage api calls. 我使用Square的改造来管理api调用。 And again, this just happens when update ui has not finished. 再次,这只是在更新ui尚未完成时发生。 When I wait for it to finish and go back everything is fine. 当我等待它完成并返回时,一切都很好。

Any ideas what might be wrong? 任何想法可能有什么问题吗?

Either abort the async operation when you navigate away from the fragment, or simply check to see if the fragment is still attached with isAdded() when you attempt to update the UI. 当您离开片段时,要么中止异步操作,要么在尝试更新UI时简单地检查一下片段是否仍与isAdded()相连。 If it's not attached just ignore the result and do not touch the views. 如果未附加,请忽略结果,不要触摸视图。

The first option will probably be the most correct but also the hardest to implement. 第一个选项可能是最正确的,但也是最难实现的。 The second option is perfectly fine in my opinion unless you're doing some really heavy and long running stuff in the background thread. 在我看来,第二种选择非常好,除非您在后台线程中执行了一些非常繁重且需要长时间运行的工作。

When I want the asynchronous task to finish anyway: imagine my onPostExecute does store data received and then call a listener to update views so, to be more efficient, I want the task to finish anyway so I have the data ready when user cames back. 当我想要异步任务完成时:想象一下我的onPostExecute确实存储了接收到的数据,然后调用侦听器来更新视图,因此,为了提高效率,我希望任务仍然完成,以便在用户返回时我已经准备好数据。 In this case I usually do this: 在这种情况下,我通常这样做:

@Override
protected void onPostExecute(void result) {
// do whatever you do to save data
  if (this.getView() != null) {
      // update views
  }
}

Fragment MyFragment not attached to Activity 未连接到活动的片段MyFragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM