show toast message even fragment is detached?

Viewed 36

in my application I have BottomNavigationView use to navigate between five fragment, frist(supposed called homefragment) fragment show progressbar while call http request with mvp architecture. if I navigate to another fragment and the homefragment fished http request and show toast measge the application crash.

1 Answers

if the logcat error says : fragment not attached to a context then,

declare this as global var..

private lateinit var appContext: Context

and in onCreate() .....

appContext = requireContext()

and in onAttach()

appContext = context

lastly .. replace all those requireContext() with appContext in that fragment

Related