Answer the question
In order to leave comments, you need to log in
How bad is it to call the activity method from the fragment directly?
There is an activity that defines the "Open screen X" method.
This method is defined in the activity, and not in the fragment, based on the fact that it must be called with a certain intent (that is, I intercept the intent in onNewIntent and call this method).
There is also a scenario where I need to open the same X screen by clicking on a button in one of the fragments. In order not to duplicate the code of the "Open screen X" method from the activity to the fragment, it seems logical to call this method from the fragment.
Here, in fact, the question is: is it necessary to fence the interface with a callback in order to inform the activity from the fragment about the need to call this method, or can you just execute ((MyActivity) GetActivity()).openScreenX() ? How bad is it to do so and what is fraught with? Thanks
Answer the question
In order to leave comments, you need to log in
I don’t see anything wrong with this if the fragment is in the FragmentManager activty and you stick to the standard android MVC, but you need to understand that at different stages of the fragment’s life, the getActivity () method can return both a reference to the activity and null.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question