9

In Android, a lot of functionality is in the Activity derived class. When an activity gets big (with many event handlers and such), the Java file can get large and very cluttered.

Is there a way to "break up" a Java class code file, like C# has the partial keyword?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 3
    We can break activity in different classes We can have external classes of Adapter, AsyncTask and so on... – rajpara Dec 19 '12 at 08:46
  • So, let's say I have some `Button`s with functionality, I could subclass those and put the subclassed `Button`s in my `Activity`? Using e.g. the `Activity.this` object to get to my `Activity`? – Bart Friederichs Dec 19 '12 at 08:49
  • You can have you own button class, and you can place that custom button in xml layout file as well as through code you can also add, second thing for button click listner , you can make one View.onClickListner custom class – rajpara Dec 19 '12 at 09:01

3 Answers3

2

short answer ? no.

quoted from wikipedia

The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows splitting a class definition into several files by using the partial keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related.

so while you may rework your design and relegate some code to utility classes to unclutter the code, you can not seperate the code of a single class across two files in java.

Oren
  • 4,152
  • 3
  • 30
  • 37
  • I knew that :). I was mainly wondering if I was the only one with this problem. I guess I have to use a different design paradigm. – Bart Friederichs Dec 19 '12 at 08:50
  • While this may be annoying. I believe its intentional. Essentially making you rethink a "god class" design. Java likes to push towards good practice while android (initiallt) seems to push against.. yup.annoying. – Oren Dec 19 '12 at 09:13
2

As others have pointed out, you cannot split the actual file (I view this as a good thing).

You can extract view related functionality in custom views and fragments. Everything else (business logic, Web service access, DB access, etc.) can be in 'helper' classes you use in your activity. Even though activities are the God objects in Android, you don't have to write everything inside the actual activity class. It should only coordinate stuff and implement necessary callbacks and event handlers (which technically can be in their own classes as well).

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Of course I put business logic and all these other things in their own classes. It is just that there is a lot of view-related functionality, making the `Activity` a little messy. I'll try the subclasses of those to see if it works for me. – Bart Friederichs Dec 19 '12 at 09:02
  • If you need to combine default widgets, fragments might be a better fit. Subclassing views is generally only useful for custom widgets. – Nikolay Elenkov Dec 19 '12 at 09:05
  • Too bad `Fragment`s are introduced in API level 11, and many handsets are still on Android 2. – Bart Friederichs Dec 19 '12 at 09:23
  • 1
    You obviously haven't heard about the support/compatibility library which lets you use fragments on earlier versions too: http://developer.android.com/tools/extras/support-library.html – Nikolay Elenkov Dec 19 '12 at 09:24
0

No. Java source codes can not be split across multiple files.

From the http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp

The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows a class definition to be split into several files, by using the partial keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related.

No_Rulz
  • 2,679
  • 1
  • 20
  • 33