Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share code between fragments

I have two ListFragments which needs to share some code. This was very basic by just creating a abstract fragment extending ListFragment and then let my two fragments both extend my abstract fragment.

However, now I need a third fragment to use the code from my abstract fragment. However, this third fragment is not a ListFragment but a GridFragment.

Is there a good way to solve this problem?

like image 545
Markus Avatar asked Oct 21 '22 10:10

Markus


2 Answers

As java won't support multiple inheritance either seperate your generic code in to Fragment Super class or make static methods.

or

Simply write a FragmentUtil class.

like image 183
Suresh Atta Avatar answered Oct 27 '22 09:10

Suresh Atta


I would suggest you put your shared code in class that dont extends any Fragments. In your fragments classes you make calls to your SharedCode.class .

like image 36
oe.elvik Avatar answered Oct 27 '22 10:10

oe.elvik