私はすべてのアクティビティで必要なデータとサービスを含むconfig構造体を持つAndroid APPを作成しました。コンフィグクラスが解放されないようにする方法
私のAPPがしばらく背景にある場合、自分の設定構造が削除されたため、APPがクラッシュすることがありました。
私の設定構造では、データもあり、実行時に簡単に再作成できません。
最初のアクティビティでは、config構造体を作成します。
FreightWeightConfig config = new FreightWeightConfig(getApplicationContext()); // make sure our config is up and running
そして、私のconfigクラスの開始私は、私が取得する必要があり、私は私のconfigクラスのインスタンスを取得することができます設定構造体の第2の機能を、持っている
public FreightWeightConfig(Context appContext) {
instance = this;
mApplicationContext = appContext;
tcBlue.setCallingContext(appContext);
tcBlueConfig = Config.getInstance(); // to make sure it is available straight away
mFirebaseAuth = FirebaseAuth.getInstance();
....
}
のように見えます設定とサービスの機能とインタフェースへのアクセス。
public static synchronized FreightWeightConfig getInstance() {
//if (FreightWeightConfig.instance == null) {
// FreightWeightConfig.instance = new FreightWeightConfig(getApplication().getApplicationContext());
//}
if (FreightWeightConfig.instance == null){
FirebaseCrash.logcat(Log.ERROR, LOG_TAG, "Fatal Error : FreightWeightConfig.getInstance()==null. Try restarting the APP");
FirebaseCrash.logcat(Log.ERROR, LOG_TAG, "Fatal Error : Killing ourself, as we have no chance to go on");
//System.exit(0); // we are in a bad state
// Toast.makeText(mApplicationContext, "Fatal Error : FreightWeightConfig.getInstance()==null. Try restarting the APP", Toast.LENGTH_SHORT).show();
}
return FreightWeightConfig.instance;
}
すべてのアクティビティで、インスタンスをコピーする変数を作成しました。これは単純に、私はそれがシステムに伝えると思ったので、私はまだこのクラスが必要です、それを殺さないでください。それはうまくいかないようです。
私は最初に、私の設定クラスが死んでいることがわかったときに、私はそれを作り直すことができると思いました。しかし、私はAPPコンテキストが必要で、バックグラウンドで私のサービスを再作成する必要があるので、単純なタスクではありません。また、私のAPPをナビゲートしながら、選択を保存する
誰も良いアイデアを持って、どのように私の設定クラスのアンロード/削除を解決するには?
public class FreightWeightApp extends Application implements DialogInterface.OnCancelListener {
private String LOG_TAG = "FreightWeightApp";
private FreightWeightConfig config;
public static int GOOGLE_PLAY_SERVIE_ABBORTED = 1001;
public void onCreate() {
super.onCreate();
// We first check if the google services are present, if not, better abort!!
int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
switch (result) {
case SUCCESS:
Log.d(LOG_TAG, "Google Services available");
break;
case SERVICE_MISSING:
Log.e(LOG_TAG, "Google Services missing, STOP");
googleServiceNotUpToDateDialog(result);
break;
case SERVICE_VERSION_UPDATE_REQUIRED:
Log.w(LOG_TAG, "Service update required");
googleServiceNotUpToDateDialog(result);
break;
case SERVICE_DISABLED:
Log.e(LOG_TAG, "Service disabled, STOP");
googleServiceNotUpToDateDialog(result);
break;
}
config = new FreightWeightConfig(getApplicationContext()); // make sure our config is up and running
}
private void googleServiceNotUpToDateDialog(int result) {
// Try to ask the user to update or finish off
// GoogleApiAvailability gaa = GoogleApiAvailability.getInstance();
// Dialog dialog = gaa.getErrorDialog(this, result, GOOGLE_PLAY_SERVIE_ABBORTED, this); //<==== Can not call this, as I have no Activity Context
// dialog.show();
}
@Override
public void onCancel(DialogInterface dialogInterface) {
// Now I should abbort the APP, or we will crash.
}
}
しかし、今、私はGoogleServiceの検証の問題を持っている:私はこのようなアプリケーションの拡張の提案に基づいて
。
Dialog dialog = gaa.getErrorDialog(this, result, GOOGLE_PLAY_SERVIE_ABBORTED, this);
私にはアクティビティがありません。
アクティビティが一時停止している状態を維持し、アクティビティが再開されたときの状態を読み取りますか? – noev