0

Except being the first Activity upon the Start of the Application, is there anything else special about main activity? From:https://developer.android.com/codelabs/android-training-create-an-activity#0

An app usually consists of multiple screens that are loosely bound to each other. Each screen is an activity. Typically, one activity in an app is specified as the "main" activity (MainActivity.java), which is presented to the user when the app is launched. The main activity can then start other activities to perform different actions.

From the quote above it seems to me that we have following hierarchy:

Activity hierarchy?

but then further is said:

Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, that new activity is pushed onto the back stack and takes user focus. The back stack follows basic "last in, first out" stack logic. When the user is done with the current activity and presses the Back button, that activity is popped from the stack and destroyed, and the previous activity resumes.

Does this also apply to the "MainActivity"? If the "MainActivity" is destroyed does that cause the App to crash i.e does the lifecycle of the MainActivity in any way differs from the lifecycle of any other activity? Is the MainActivity the last activity that is being stopped when the App is exited?

Why do I need this:

I would like to free some resources when the App is exited (in the onStop() Method (because post Honeycomb it is guaranteed that onStop will be called)), especially ExecutorServices, I've read here that even though App is exited there is no guarantee that an ExecutorService will be stopped and will make JVM continue working/running, even though App is closed/killed and will continue to use system resources.

  • 2
    just a name nothing different from others – Majid Ali Dec 24 '20 at 08:34
  • not really, its the default. – Gil Caplan Dec 24 '20 at 08:36
  • 1
    Its default because Android Studio has a built in `template` which names the starting/initial activity as `MainActivity`, nothing else. You can pretty much name it anything you want, `NotSoMainActivity`, `SplashActivity`, just change respective activity in the manifest. – Darshan Dec 24 '20 at 09:30

1 Answers1

1

Main Activity is the entry point for your app when user presses the icon for cold launch. You make any of your Activity a main activity in AndroidManifest.xml file via Intent Filter. Intent Filter tells the system which activity is main.

Although main activity is considered first entry point typically, but keep in mind, Main Activity is not always the first activity to be launched, for example there are various intent filters that can be assigned to your other Activity and that activity can be opened directly following the related action. Please read about the Intent-Filter here.

For example, your app is gallery app, typical first screen would be album list. from where you can view individual photo in PhotoActivity. This PhotoActivity can be opened directly via intent from outside apps to view a certain photo without needing to launch the main activity. (Check out google Photos app)

Regarding ExecutorServices or other services lifecycle, few options here:

  1. implement a ownership machanism, ie the activity that starts the service is responsible for closing the service
  2. You can monitor your app's activity stack and kill the service when your Activity stack is empty.
  3. Leverage Application class lifecycle to monitor things.
  4. Reasonable discussion here https://stackoverflow.com/a/5862048/593709
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153