What is the best equivalent of a main() function for an Android app?

孤人 提交于 2019-12-11 02:08:59

问题


I've searched everywhere a manner to have the equivalent of a main() function (yes function not method) inside a Android application but failed...

Typically what I would want to do is:

void main()
{
    // do some really nice initialisations stuff here

    // ... let the app does his life, I really don't care

    // do some final stuff here before leaving
}

The nearest approach I've seen so far is to use a SplashScreen and override the OnCreate() method. The problem is that is not acceptable from my point of view. Why? Because a SplashScreen is nothing than an Activity tagged as a LAUNCHER.

That makes it to appear in the apps list, thing I don't want when I develop an app widget. Furthermore, where to place my code just before the app destroy? In the onDestroy() method? No, once again, this is not reliable. Android can decide to delete my instance whereas the application is still running.

Well, in fact, I take for principle that every components of my app are running in the same process since I don't mention explicitely in the Manifest that I wan't a component to run in its own process.

In the case of an app widget, I've placed my init code on the first call of onUpdate() method. I think it's a good bet. Then this app widget (AppWidgetProvider more precisely) is in charge to launch any activity as its will.

The "DataBase" for all the app is defined in a separate Singleton like this:

public class MyDataBase {

    public static MyDataBase getInstance() {
        if (instance_ == null)
            instance_ = new DataBase();
        return instance_;
    }

    public void load();
    public void save();

    static MyDataBase instance_ = null;

    public int myInt;
    public String myString;
    public Object myObject;
    etc..
}

With this Singleton I'm sure at least, its lifecycle is the same as the entire app itself.

To back with that AppWidgetProvider, I have to trick a little. Indeed, Android can decide to delete its instance whereas some other activities are still on place and the process is still running. So for example, systematically loading my DataBase in the first call of the OnUpdate() is unnecessary and overkill. What I do is having a static boolean value that indicates if the DataBase have been loaded for the lifecycle of this process or not. Thus, the AppWidgetProvider can be instanciated tons of time, as long as the Singleton DataBase persists (so the process), it will not reload the DataBase each time, got it? (yes difficult to be clear...)

About the cleanup code of the app, I thought to override the finalize() method of my DataBase Singleton, but well, I'm really not sure it's a good idea since the moment of the call of this method is totally unpredictable. I suppose it would be called if you suddently power off your Android, but well I'm not sure of anything here, thus so far, I didn't found a solution for that part.

Any comment or something less tricky that what I currently do is welcome. Thanks.


回答1:


onResume() is the function that will be invariably reached before starting you app, so you could either put the 'main' code in the onCreate() method or the onResume().

onPause() is ALWAYS called before destroying the app, either by the user or the OS.

There is great explanation regarding the lifecycle in the Android documentation:

http://developer.android.com/training/basics/activity-lifecycle/starting.html




回答2:


For the initialisation you can over-ride the onCreate method of the Application class:

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process. If you override this method, be sure to call super.onCreate()

Termination is harder to deal with. You'll probably have to monitor each component of your application separately. If you are targeting API level 14 or later you can use Application.registerActivityLifecycleCallbacks to help with this.




回答3:


The nearest approach I've seen so far is to use a SplashScreen and override the OnCreate() method. The problem is that is not acceptable from my point of view. Why? Because a SplashScreen is nothing than an Activity tagged as a LAUNCHER.

It's because Android is formed with several activities and those activities have life cycles. So every activities start from onCreate() then finishes at onDestroy(). http://developer.android.com/training/basics/activity-lifecycle/starting.html

That makes it to appear in the apps list, thing I don't want when I develop an app widget. Furthermore, where to place my code just before the app destroy? In the onDestroy() method? No, once again, this is not reliable. Android can decide to delete my instance whereas the application is still running.

In a scenario that user presses home button to exit from your app, your current activity is more likely to invoke onPause() method (only when the activity has no other processes to finish). However, when user force closes (terminates) your whole application by ending process. Then you don't have to worry about invoking any methods or what so ever because Android itself will close anything related to your application automatically.

To back with that AppWidgetProvider, I have to trick a little. Indeed, Android can decide to delete its instance whereas some other activities are still on place and the process is still running. So for example, systematically loading my DataBase in the first call of the OnUpdate() is unnecessary and overkill. What I do is having a static boolean value that indicates if the DataBase have been loaded for the lifecycle of this process or not. Thus, the AppWidgetProvider can be instanciated tons of time, as long as the Singleton DataBase persists (so the process), it will not reload the DataBase each time, got it? (yes difficult to be clear...)

The example you have posted for singleton database connection is not bad I think, but there are better ways to get the job done cleanly and more effectively. For example hibernate framework connection pooling



来源:https://stackoverflow.com/questions/14423782/what-is-the-best-equivalent-of-a-main-function-for-an-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!