In Java applications, as I understand it, there is a single entry point for the entire application in the form of the main method, but this method is not in Android applications. How is single entry point implemented in Android applications and is it there?
6 answers
Android applications do not have a single entry point, as is done for example with the help of main() . An application can run in so many ways. Applications, depending on their functionality, may consist of different components of activation, services.
The “normal” way, of course, is the entry point through main activit, but it may not always be the only one. The launch of one or another component depends on the events that occur in the system and on whether the application can handle these events.
For example, a service can "listen" to an ACTION_BOOT_COMPLETED event and process it to start immediately after the operating system is loaded, etc. Intent and BroadcastReceiver in terms of selecting the entry point into the application provide quite ample opportunities.
- But unless before all actions of application
Application#onCreate()will not be caused? - Yuriy SPb ♦ - onefrom the point of view of the conceptual launch of the application by the operating system itself, you are certainly right, but the entry point to the application itself, for the further execution of its logic, depends on the events that caused it - ZigZag
Taki do not agree with @ YuriySPb
From the point of view of the GUI, the entry point is the Activity marked / indicated in the manifest as:
<intent-filter> <action android:name="android.intent.action.MAIN/> </intent-filter> It is what determines which Activity will be launched, if you poke into the application icon.
For android, the Application class and its onCreate() method are onCreate() . In this case, it must be prescribed in the manifest.
Any Activity flagged in manifest as
category android:name="android.intent.category.LAUNCHER" action android:name="android.intent.action.MAIN is the entry point to the application. The Application class is called first when the program starts. The onCreate method enters the life cycle of any Activity .
The Android application is modular and may include service, reciever, and other elements that can be run before or separately from activity.
Depending on the task, a simple option may be suitable for you - onCreate method of Activity specified as <action android:name="android.intent.action.MAIN/> In the case of the same ACTION_BOOT_COMPLETED receiver, this will be onRecieve. In the case of the onCreate service .
In general, you can specify the application class in the manifest:
<application android:name=".App" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:theme="@style/AppTheme"> And to inherit applications from Application or similar.
public class App extends MultiDexApplication { @Override public void onCreate() { //ваши действия } } You need to create a class that inherits from the Application class. Override the onCreate method in it, remembering to call the parent method. This method will work first when opening the application.
In the manifest, add the name parameter with the above class to the application tag.
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // делаем что-то } } <application android:name=".MyApplication">