The solution is to use one (or more) helper classes associated with your activity;
of course the helper class will have to see all the properties of your Activity for use them.
A way that I found to do this is as follows:
Suppose you have a MainActivity.java
-
Create a class HelperActivity.java that accepts in its constructor the instance of main activity class:
public class HelperActivity{ private MainActivity act; public HelperActivity(MainActivity activity) { this.act = activity; } ... }
-
Instantiates the helper class from MainActivity, passing itself:
public class MainActivity extends Activity { private HelperActivity helper = null; protected void onCreate(Bundle savedInstanceState) { ... //init l'helper helper = new HelperActivity(this); //... } }
-
To make sure that the class helper has access to the various objects of the class MainActivity you can enter the two classes in the same package and declare attributes as protected:
public class MainActivity extends Activity { protected TextView mText= null; ... } public class HelperActivity{ ... activity.mText.setText("this is a text"); }
solved the Activity’s source code is too long, what I do? [closed]