LoginSignup
2
5

More than 5 years have passed since last update.

FragmentのonResumeが2回呼ばれる対策

Posted at

Fragmentを追加(add)して、削除(popBackStack)して、同一のFragmentを再度追加(add)すると、
FragmentのonResume()が2回呼び出されることがある。

参考リンク

Why is my onResume being called twice?

Android lifecycle onPause and onResume are called twice in some test-cases in both android lollipop version 5.0 and 5.1.

原因の予想

同一のFragmentを再度作成し追加(add)する際に、
古い(削除した)Fragmentと新しい(追加した)Fragmentの両方のonResume()が呼ばれてしまうのではないかと予想している。

回避策

onResume()が2回呼ばれる場合があるが、
そのうちの一つはonResume()が呼び出されているのにisAdded()がfalseとなるケースがある。
FragmentのonResume()の最初にisAdded()のチェックを行うことで、
異常ケースのonResume()の処理を弾くことが出来る。

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5