如何访问应用程序的开始(不是活动) [英] How to access a start of Application (not Activity)

查看:138
本文介绍了如何访问应用程序的开始(不是活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序的开头实现非常具体的代码.

I need to implement very specific code in the start of the application.

我的意思是,不是在活动(onCreate()或onStart())的开始,而是在应用程序的开始.

I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.

我有一个不利于我的解决方案,那就是拥有一个名为"MyBaseActivity"的基础活动,然后在我的所有活动中都从中进行扩展. 这种解决方案对我不好,因为这种解决方案使我只能在每个活动的onCreate中执行一件特定的事情(我所谈论的特定代码),这不是我想要的.

I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities. This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.

我希望每个活动都能够根据其onCreate()函数执行不同的操作,并希望执行上面我所讨论的特定代码.

I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.

因此,我需要访问应用程序的开始,或者您有其他解决方案.

Therefor, I need to access the start of the application, or that you have another solution for me.

谢谢!

推荐答案

创建应用程序/程序包的过程时,将在任何其他类之前实例化Application class或Application类的子类. 您需要扩展应用程序类.

The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created. You need to extend application class.

public class AppApplication extends Application{
 @Override
    public void onCreate() {
        super.onCreate();
        //Do whatever you want 
    }
}

此AppApplication类应包含在清单文件中.

And this AppApplication class should be included in manifest file.

<application
        android:allowBackup="true"
        android:name=".AppApplication"
        android:icon="@mipmap/ic_launcher"

这篇关于如何访问应用程序的开始(不是活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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