2017-07-17 9 views
0

私はすべてのアクティビティで必要なデータとサービスを含む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); 

私にはアクティビティがありません。

+0

アクティビティが一時停止している状態を維持し、アクティビティが再開されたときの状態を読み取りますか? – noev

答えて

0

Applicationの独自の実装を作成し、onCreate()メソッドで設定オブジェクトを初期化します(この場合、getInstance()メソッドはオブジェクトを初期化できます)。

public class MyApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     FreightWeightConfig config = FreightWeightConfig.getInstance(getApplicationContext()); 
    } 
} 

、アプリのモジュールマニフェストであなたの実装を宣言します。

<?xml version="1.0" encoding="utf-8"?>           
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package">            

    <application 
     android:name="your.package.MyApplication"                
     android:icon="@mipmap/ic_launcher"          
     android:label="@string/app_name"          
     android:theme="@style/AppTheme"> 
     ...    

    </application>                

</manifest>                  

今、あなたはのgetInstance()を呼び出して、あなたが同じような活動にあなたのconfigインスタンスを使用することができます。アプリケーションonCreateはロード作業を行います。しかし、アプリプロセスが強制終了/キャッシュされる前にリソースをアンロードする方法はないので、Androidコンポーネントのライフサイクルの仕組みを理解するだけで済みます。

+0

私はその考えが好きですが、自分のConfigを作成する前に、自分のGoogleServiceが最新であるかどうかをチェックする必要があります。それ以外の場合はクラッシュします。私は私のConfigでFireBaseデータベースなどのGoogle APIを使用しています。 今まで、私は getErrorDialog(Activity、int、int、DialogInterface。OnCancelListener); 私はActivity Contextを持っていないので、もうちょっと立ち往生しています。 –

+0

1 DialogInterface実装をApplicationクラスから削除します。 2 - アプリケーションのonCreateメソッドからすべてのGoogleコードを削除します。 3 - あなたの設定クラスにあなたのGoogleコードを入れてください。 4 onCreateメソッドの設定をシングルトンとして初期化します。 5あなたの活動とあなたの設定のコールバックでDialogInterfaceを実装します。 6-あなたのアクティビティからあなたの設定を呼び出します:FreightWeightConfing.genInstance()。isPlayServicesOk(this)これは、プレイサービスのロジックが完了したときにconfigがコールするコールバックのアクティビティ実装です。 –

+0

もし私があなたが言うように、私は私の元の問題に戻る。 手順4を実行して、私がバックグラウンドでしばらくいると、OSがkillして自分の設定を解放する傾向があります。 そのため、私はApplicationクラスで新しいアプローチを行ったのです。 私の設定は作成時にGoogle(Firebase)コードにアクセスする必要があります。 –

関連する問題