在启动 Activity 之前,您是如何 运行 编码扩展应用程序的?
Is extending Application how you run code before the launching Activity?
如果我这样做:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//init something else here if you want
}
@Override
public void onTerminate() {
super.onTerminate();
//terminate something else here if you want
}
}
并在清单文件中包含此 class 的名称,如下所示:
<application
android:name="com.packagename.MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
这是否有效地为我们提供了一种方法 运行 在应用 运行 之前和之后我们想要的任何代码?
编辑: 如果我进入 onCreate()
语句,我会在代码中看到:
/**
* 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().
*/
@CallSuper
public void onCreate() {
}
/**
* This method is for use in emulated process environments. It will
* never be called on a production Android device, where processes are
* removed by simply killing them; no user code (including this callback)
* is executed when doing so.
*/
@CallSuper
public void onTerminate() {
}
编辑 2: 我还可以将应用程序上下文保存为全局静态变量:
public class MyApp extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
不是之前和之后,而是整个 Application
生命周期,例如所有 运行ning Activity
s、Service
s 和其他上下文生物...如果 none 当前是 visible/running Android 系统可能总是从内存中删除你的 Application
(用户也是)。
如果您正在寻找 运行 screen/without 任何 UI 之外的一些代码的方法,请查看 Service class 或其他延迟警报-基础方法。
你不能依赖 subclassing Application
class 因为你甚至不知道它什么时候被 OS "automatically" 杀死.
是的。
让它扩展应用程序的主要原因 class
- 就是把你想做单例的都初始化
在整个应用程序中并在组件中使用。
- 有一些静态变量
跨组件使用
如果我这样做:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//init something else here if you want
}
@Override
public void onTerminate() {
super.onTerminate();
//terminate something else here if you want
}
}
并在清单文件中包含此 class 的名称,如下所示:
<application
android:name="com.packagename.MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
这是否有效地为我们提供了一种方法 运行 在应用 运行 之前和之后我们想要的任何代码?
编辑: 如果我进入 onCreate()
语句,我会在代码中看到:
/**
* 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().
*/
@CallSuper
public void onCreate() {
}
/**
* This method is for use in emulated process environments. It will
* never be called on a production Android device, where processes are
* removed by simply killing them; no user code (including this callback)
* is executed when doing so.
*/
@CallSuper
public void onTerminate() {
}
编辑 2: 我还可以将应用程序上下文保存为全局静态变量:
public class MyApp extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
不是之前和之后,而是整个 Application
生命周期,例如所有 运行ning Activity
s、Service
s 和其他上下文生物...如果 none 当前是 visible/running Android 系统可能总是从内存中删除你的 Application
(用户也是)。
如果您正在寻找 运行 screen/without 任何 UI 之外的一些代码的方法,请查看 Service class 或其他延迟警报-基础方法。
你不能依赖 subclassing Application
class 因为你甚至不知道它什么时候被 OS "automatically" 杀死.
是的。 让它扩展应用程序的主要原因 class
- 就是把你想做单例的都初始化 在整个应用程序中并在组件中使用。
- 有一些静态变量 跨组件使用