Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MainActivity is too big. How do I split it into multiple files

I'm building an Android App which has a complex screen with lots of logic.
It currently contains a listview, tabs, search box, and a panel for updating user stuff.
The probem is that the mainactivity code file became very big, although I'm seperating things to different layers, because there's a lot of UI components which affect things in the screen.

I'm trying to seperate it to several files but I don't seem to do it right.
As much as I understood Fragments is not what I need here. Are there any other ways?
Just need some directions please.

I'm asking mostly about the code, not the layout (Although I don't care changing the layout too).

Currently it's 616 lines and the biggest problem is that we are a team and the maintenance became hell...

like image 905
Hagai L Avatar asked Oct 22 '14 08:10

Hagai L


2 Answers

Create utility class and put your listeners and adapters there. Use main activity only to initialise view instances and setting listeners and adapters.

like image 62
ash Avatar answered Oct 06 '22 00:10

ash


Although 616 is not a particularly big file you could use Fragments as they just add another layer of abstraction. They also have a similar lifecycle to activities.

This tutorial shows how to add fragments to tabs

http://developer.android.com/training/implementing-navigation/lateral.html

like image 40
jiduvah Avatar answered Oct 06 '22 01:10

jiduvah