In most cases, when developing a desktop application, I like to make the main application as a singleton for convenience. I can easily access application data and methods without having to pass the main link to the application.
public class MainFrame extends javax.swing.JFrame {
However, from the point of view of the Android platform, I am not sure if this is / is safe to do, since I do not have direct control over the creation of MainActivity . MainActivity I will have
- Startup mode will be
standard . - The only time an instance of
MainActivity is created is when the user clicks the application icon. So, the only way to launch it is indicated in the AndroidManifest.xml <application> . There MainActivity.apk not be any other Java code in MainActivity to run MainActivity itself.
public class MainActivity extends Activity { public static MainActivity INSTANCE = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); assert(INSTANCE == null); INSTANCE = this; } }
source share