Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragments hide show OnHide/OnShow?

I am asking this cuz I am sort of curious.

1 ) Most google demos finds fragments by its ID if the fragment is already been created in xml.

So if we take that approach, the way we show fragments is by hiding it and showing it since the fragments are already created.

2) There are also examples provided by google where you can create the fragment with a constructor and inflate it. This acts weird by the way like getActivity() returns null if it is called with in that fragment.

So If i take the first approach I have to hide and show the fragments. So why does not google provide hooks to the fragments like onHide or onShow so that we can handle things properly instead if doing the clean up ourselves with functions that we implement and call explicitly.

like image 284
user498584 Avatar asked Aug 15 '11 18:08

user498584


People also ask

How do you hide a fragment?

Showing and hiding fragment's viewsUse the FragmentTransaction methods show() and hide() to show and hide the view of fragments that have been added to a container.

Are fragments visible?

Fragment is added and its layout is inflated. Fragment is active and visible.

How do you identify a fragment tag?

Often we need to lookup or find a fragment instance within an activity layout file. There are a few methods for looking up an existing fragment instance: ID - Lookup a fragment by calling findFragmentById on the FragmentManager. Tag - Lookup a fragment by calling findFragmentByTag on the FragmentManager.

Which method is called once the fragment gets visible?

onStart()The onStart() method is called once the fragment gets visible. onResume()Fragment becomes active.


1 Answers

If you want to hook op on onHide/onShow just override

public void onHiddenChanged(boolean hidden) {
}

in your fragment.

like image 79
Thomas Ahle Avatar answered Oct 10 '22 04:10

Thomas Ahle