How to break Android activity in multiple files

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

Is there a way to β€œsplit” a Java class code file, for example, C # has a partial keyword?

+7
source share
3 answers

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

You can display viewing-related functions in custom views and snippets. Everything else (business logic, access to web services, access to the database, etc.) can be in the "auxiliary" classes that you use in your work. Despite the fact that actions are objects of God in Android, you do not need to write everything inside a real activity class. It should only coordinate the data and implement the necessary callbacks and event handlers (which technically can be in their own classes).

+1
source

short answer? not.

cited by wikipedia

The Sun Microsystems Java compiler requires that the source file name matches only the public class within it, and C # allows several public classes in the same file and does not contain restrictions on the file name. C # 2.0 and later versions allow you to split a class definition into multiple files using an incomplete keyword in the source code. In Java, an open class will always be in its own source file. In C #, source code files and logical unit separation are not related to each other.

therefore, when you can redo your design and cancel some code for utility classes to unlock the code, you cannot separate the code of one class from two files in java.

+2
source

Not. Java source code cannot be split into multiple files.

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

The Sun Microsystems Java compiler requires that the source file name matches only the public class within it, and C # allows several public classes in the same file and does not contain restrictions on the file name. C # 2.0 and later versions allow you to split a class definition into multiple files using an incomplete keyword in the source code. In Java, an open class will always be in its own source file. In C #, source code files and logical unit separation are not related to each other.

0
source

All Articles