Search code examples
javaandroidandroid-lifecycle

How to call method from fragment only if host activity was destroyed?


I got a method() in the fragment. Fragment in host activity. So I need to call the method() from fragment ONLY when host activity was in onDestroy() . Maybe it suppose to be something like a static flag?


Solution

  • You can, by SupportFragmentManager, get a Fragment by layout id, tag or name. But the most important thing here is in some situations Activity.onDestroy() method could never be called. So be careful if you implement code that needs to be executed, like an unsubscribe logic or removing callbacks.