What is the logic before super.method or after super.method in android?

Hi everyone, I want to ask what is the difference if I write something before super.onDestroyView (); and after super.onDestroyView (); see example below

Delete fragment before super.ondestoryview ();

@Override public void onDestroyView() { try { Fragment fragment = (getFragmentManager() .findFragmentById(R.id.mapviews)); FragmentTransaction ft = getActivity().getSupportFragmentManager() .beginTransaction(); ft.remove(fragment); ft.commit(); } catch (Exception e) { e.printStackTrace(); } super.onDestroyView(); } 

Delete fragment after super.ondestoryview ();

 @Override public void onDestroyView() { super.onDestroyView(); try { Fragment fragment = (getFragmentManager() .findFragmentById(R.id.mapviews)); FragmentTransaction ft = getActivity().getSupportFragmentManager() .beginTransaction(); ft.remove(fragment); ft.commit(); } catch (Exception e) { e.printStackTrace(); } } 
+6
source share
5 answers

If super was Fragment, then there is no difference how you do it, because Fragment onDestroyView does nothing. But in some cases it matters.

As Diana Hackborn said:

general rule: during any initialization, let the superclass do its work first; during any finalization you do your first job

PS IMHO this is a bad solution for removing a fragment from another method of the onDestroyView fragment. Strange, I think you should find the best place to manage your fragments ...

+4
source

Helpful answer

case 1: if there is code written in super.onDestroyView, then this code will be executed after the code you wrote.

case 2: if there is code written in super.onDestroyView, then this code will be executed first, then the code you wrote will be executed.

0
source

Here is the documentation for Fragment.java onDestroyView ():

 /** * Called when the view previously created by {@link #onCreateView} has * been detached from the fragment. The next time the fragment needs * to be displayed, a new view will be created. This is called * after {@link #onStop()} and before {@link #onDestroy()}. It is called * <em>regardless</em> of whether {@link #onCreateView} returned a * non-null view. Internally it is called after the view state has * been saved but before it has been removed from its parent. */ @CallSuper public void onDestroyView() { mCalled = true; } 

The important line of this documentation is: Internally it is called after the view state has been saved but before it has been removed from its parent.

If your onDestroy() method does not need any changes to the views that will be saved, then I think it does not matter when you call super() .

0
source

Your code should, as a rule, do its work after the originally planned work has been done - the caveat here is that super work changes the state of what you want to work with. However, it comes down to different cases - read the code super - sometimes it is already beyond something else.

0
source

onDestroy () super should be called as soon as you are done with your cleanup processing. Its a good coding practice and a reason for less buggy programming.

0
source

Source: https://habr.com/ru/post/979310/


All Articles