I am new to Java & Android development, but I have been able to create a mess of functions, subclasses & constants that work perfectly fine when pasted inside the MainActivity class of a default Android project.
The purpose of these functions is to “ping home” to let my servers know when an application install has occurred. The only code that is needed to make this happen is the line notifyMyServer() inside this standard block of code of MainActivity.java:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyMyServer("install"); /* Notify MyServer of an Installation */
}
}
Pasting the functions, subclass, constants and imports that make notifyMyServer() work directly inside the MainActivity.java file seems dirty to me. I would love to be able to move them to their own file and/or library so I could drag & drop that file into other apps I want to do perform the same tracking on.
How is this typically done?
Take your notify code and create an class which just performs this notification (can be part of a library or not):
And then in your MainActivity, just inherit from
NotifyingActivityinstead ofActivity. Then the notification will automatically happen when you call super.onCreate() inside of MainActivity.