Skip to content Skip to sidebar Skip to footer

Why No Main() Method Present In Android

I am asking this because after reading that , android depend upon components thats why they have removed main() method from, but can we not create a program by adding main() at one

Solution 1:

At AndroidManifest.xml you need a LAUNCHER activity, and 2 more for network and no network like:

<activityandroid:name="your.package.StartActivity" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="your.package.NetworkActivity" ></activity><activityandroid:name="your.package.NoNetworkActivity" ></activity>

At StartActivity.java check connectivity and then choose what activity to start:

publicclassStartActivityextendsActivity {
     //...protectedvoidonCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         // here do the job!!!booleaniAmConnected= checkNetwork(); // you need to implement thatif(iAmConnected) {
           // start network activityIntentintent=newIntent(this, NetworkActivity.class);
           startActivity(intent);

         } else  {
           // start no network activityIntentintent=newIntent(this, NoNetworkActivity.class);
           startActivity(intent);
         }

     }

    //...
 }

StartActivity can have a setContentView(R.layout.a_splash_screen); or no content view.

Solution 2:

main() method is only entry point for many languages , same like java android expects some entry point to start the Application (first screen) , in android we will specify which activity has to be started in android manifest file using Intent Filter

<categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter>

when the activity started (First Screen is visible to User) it will call the onCreate(Bundle b) method ...activity life cycle methods

Post a Comment for "Why No Main() Method Present In Android"