什么是main()函数的一个Android应用程序的最佳等同? [英] What is the best equivalent of a main() function for an Android app?

查看:200
本文介绍了什么是main()函数的一个Android应用程序的最佳等同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜寻的方式有一个main()函数的等效(是不是函数法)一款Android应用程序中,但未能...

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

通常我会想要做的是:

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
}

到目前为止我见过的最接近的方法是使用一个闪屏和覆盖onCreate()方法。问题是,是不是从我的角度来看可以接受的。
为什么?由于闪屏无非是标记为一个启动一个活动。

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.

这使得它在应用程序列表中出现,事情我不想当我开发一个应用程序部件。
此外,如果把我的code中的应用摧毁之前?在的onDestroy()方法?
否,再次,这是不可靠的。 Android版可以决定删除我的实例,而应用程序仍在运行。

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.

唉,其实,我承担原则,即我的应用程序的每个部件在同一进程正在运行,因为我不认为我wan't在它自己的进程中运行一个组件清单明确地提了。

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.

在一个应用程序窗口小部件的情况下,我已经放置的onUpdate的第()方法调用初始化我的code。我认为这是一个不错的选择。那么这个程序小部件(AppWidgetProvider更多precisely)负责开展任何活动作为它的旨意。

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.

要与AppWidgetProvider回来,我都要骗一点点。事实上,Android的可以决定删除它的实例,而一些其他的活动仍然在地方,这个过程仍在运行。因此,例如,系统加载我的数据库中的OnUpdate()的第一个电话是不必要的,矫枉过正。我要做的就是有一个静态布尔值,表示如果数据库已加载此过程,不是的生命周期。
因此,AppWidgetProvider可以被实例化大量时间,只要辛格尔顿数据库仍然存在(这样的过程中),也不会每次都重新加载数据库,懂吗?
(是很难清楚...)

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...)

关于应用程序的清理code,我想重写我的数据库辛格尔顿的finalize()方法,但很好,我真的不知道这是因为这种方法的调用的时刻是个好主意完全取消predictable。我想这会被称为如果您suddently你的Andr​​oid断电,但还有我不知道的东西在这里,因此到目前为止,我没有发现该部分的解决方案。

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.

推荐答案

onResume()被认为将应用程序开始你之前总是达到的功能,所以你既可以把'主'code在OnCreate( )方法或onResume()。

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()被破坏之前的应用程序总是叫,或者由用户或操作系统。

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

有是关于Android文档生命周期中的很好的解释:

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

<一个href=\"http://developer.android.com/training/basics/activity-lifecycle/starting.html\">http://developer.android.com/training/basics/activity-lifecycle/starting.html

这篇关于什么是main()函数的一个Android应用程序的最佳等同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆