在onCreate()之前做些什么? [英] Do something before onCreate()?

查看:143
本文介绍了在onCreate()之前做些什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用MainActivity的 onCreate()之前是否可以做一些事情?

Is there a way to do some stuff before onCreate() of the MainActivity is called?

我们想做一些初始化工作,例如日志记录...

We want to do some initializing stuff like logging...

推荐答案

两个选项;

1.如果您的日志记录与即将开始的活动无关,则需要在第一个活动开始之前进行一些"初始化,然后子类 android.app.Application .在您的应用程序启动时,这里的 onCreate 方法几乎是第一件事.

1 . If your logging is not related to the activity about to start, rather, you'd need 'some' initialisation before the first activity starts then subclass android.app.Application. The onCreate method here is pretty much the first thing to run when your application starts.

例如,在我们的应用程序中,这是我们创建DI注入器的位置,或者确定该应用程序是否需要创建的数据库(准备首次使用")类型的任务.

For example, in our app this is where we create our DI injector's or decide whether the app needs a database created ("Preparing for first use") type tasks.

这似乎非常适合应用程序级日志子系统的初始化...

This seems like a good fit for application wide logging subsystem initialisation...

2.如果不这样做(并且如果您想在调用Activity的 onCreate 方法之前恰好记录日志),那么这是Aspects的经典用例.出于类似原因,我们将AspectJ与数据库事务管理一起使用.请参阅此博客条目如何在Android构建系统中编织所需的代码.

2 . Failing that (and if you want to log precisely just before an Activity's onCreate method is called) then this is a classic use-case for Aspects. We use AspectJ for similar reasons alongside database transaction management. Refer to this blog entry on how to weave the code you require in the Android build system.

这篇关于在onCreate()之前做些什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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