我有一个android应用程序,它使用几个单例对象。这些对象在许多活动中使用。当在活动中抛出异常时,我的单例中的不同字段变为null,所有其他活动也抛出异常。例如,我有一个单例类来处理我的cookie,它有一个静态的HTTPCookie
字段,如果在任何活动中发生异常,这个cookie字段也会变成null
。
更新:我的一个单例的代码。
public class CookieHandler {
public interface CookieHandlerCallback {
void OnNewCookieReceived();
void OnNewCookieFailed();
}
public static HttpCookie myCookie;
private static CookieHandler singleton = new CookieHandler();
private static CookieHandlerCallback cookieHandler;
private static Context context;
public static CookieHandler getInstance() {
return singleton;
}
private CookieHandler() {
}
public HttpCookie getMyCookie() {
return myCookie;
}
public static void setupInterface(CookieHandlerCallback co) {
cookieHandler = co;
}
public static void getNewCookie() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = prefs.getString("cookie", "");
myCookie = gson.fromJson(json, HttpCookie.class);
AppidoRetroAPI apiRetro = RetrofitService.getAppidoAPI();
Call<ResponseBody> call = apiRetro.getCookie(myCookie == null ? "" : myCookie.getValue());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 401) {
Log.d("app process", "my debug" + "requestCategories in code 401 ");
String headerCookie = response.headers().get(ApiCnst.COOKIES_HEADER);
if (headerCookie != null) {
myCookie = new HttpCookie(ApiCnst.MY_COOKIE_NAME, headerCookie);
Gson gson = new Gson();
String json = gson.toJson(myCookie);
myCookie = gson.fromJson(json, HttpCookie.class);
editor.putString("cookie", json);
editor.commit();
}
} else if (response.code() == 200) {
UserProfileInfo.getInstance().setIs_logged_in(true);
try {
JSONObject jsonObject = new JSONObject(HttpManager.JsonTOString(response.body().byteStream()));
UserProfileInfo.getInstance().setUserJson(jsonObject);
} catch (Exception e) {
e.printStackTrace();
}
Log.d("app process", "my debug" + "requestCategories in code 200 ");
}
cookieHandler.OnNewCookieReceived();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
cookieHandler.OnNewCookieFailed();
}
});
}
public static void updateContext(Context ctx) {
context = ctx;
}
当你的应用程序崩溃时,整个应用程序进程重新启动,即单例也得到重置,你不能阻止你的单例变为null当应用程序崩溃时,但你可以初始化当你的应用程序全局应用程序上下文创建。
App.javapublic class App extends Applications{
@Override
public void onCreate() {
super.onCreate();
initializeSingletons();
}
private void initializeSingletons(){
// Initialize Singletons.
}
}
在manifest
中注册这个类<application
android:name=".App" // Register here
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
.
.
.
这将保证在所有事情之前调用,即在广播,活动,服务等之前,所以你可以确保你的单例在应用程序崩溃和重启时永远不会为null。
当一个应用程序崩溃时,这个应用程序的jvm会重新启动,你的类会被重新加载,你会丢失所有的静态变量和实例变量。
更多信息你可以阅读这个问题Android静态对象生命周期
当任何活动抛出异常时,它停止并重新创建,而不重新创建父活动,这就是为什么您的单例对象变为空