Question

I know this is a very common topic. I have found many solutions, but none that I really want. I found this link http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ to be very helpful but I do not understand something. In that example, tabs which are labelled 'Top Rated , Games, Movies' all extend Fragment How can use my activity events on those classes? I mean, on games.java class how can I use onTouch method or onCreate or something which needs to extend activity?

Thanks.

EDIT I want to use like this ; in my mainactivity I define 3 tab class(A,B,C) ; And when I swipe to B I want to assign xml(buttons ,lists...) to B and I want to onClick listeners, events on B.java class, but it extends fragment not activity.

Was it helpful?

Solution

This is the purpose of Fragments. The Fragment lifecycle is very similar to the Activity lifecycle and I definitely recommend that you read up on it here. An Activity is designed to be the controller which holds the glue of the fragments inside of it. In practice, many activities are mostly containers for the fragments that do all the work. Also, within a Fragment, you can always get a reference to its activity with getActivity(), so just about anything that can be done with an Activity can be done with a Fragment.

OTHER TIPS

All three classes which extends Fragment make them "Singleton" means static instance . Now if you want to use ontouch method or any method of Activity you create same method on all 3 fragment classes. then call on Activity method through its fragment instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top