T
T
Tsuzukeru2020-09-21 22:07:01
Android
Tsuzukeru, 2020-09-21 22:07:01

Why does fragmentmanager get destroyed after config change?

There is a fragment that is attached to Activuty as follows.

override fun onAttach(context: Context) {
        super.onAttach(context)
        try{
            activityContract = context as IActivity
        }catch (e:ClassCastException){
            throw ClassCastException(context.toString() + "Активити должна реализовывать интерфейс IActivityView")
        }
    }


IActivity is the interface that my activity implements. It contains methods for managing the Progress Bar element and a method for creating an ErrorDialogFragment.
By reference activityContract I call the methods of Activity. They all work, but after rotating the screen in one method that uses the FragmentManager, an exception is thrown:
illegalstateexception: fragmentmanager has been destroyed


I don't understand why this is happening... In the onCreate() method, the Activity() fragmentManager is initialized.
lateinit var fragmentManager:FragmentManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_search)
        fragmentManager = supportFragmentManager
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tsudzukeru, 2020-09-29
@Tsuzukeru

The problem arose due to the fact that I did not unbind the fragment from the Activity when changing the screen configuration.
In the fragment's onDestroy() method, you need to nullify the reference to the Activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question