アプリケーションが閉じられていない(バックグラウンドで実行されている)場合、アプリケーションがクラッシュしますか?

LMK

アプリケーションはエミュレーターとモバイルでうまく機能しますが、電話の終了ボタンをクリックしてアプリケーションを閉じると(アプリケーションからではなく)、数時間後にアプリケーションを再度開くと、アプリケーションの途中から開かれます(最初の画面から)そしてそのアプリを使用した後、何度かハングしてメッセージが表示されます '残念ながらアプリは停止しました'。これはモバイルの問題ですか、それともアプリケーションの問題ですか。

リオールオハナ

アクティビティのドキュメントを読むことをお勧めします。Android OSには、独自のアプリケーションライフサイクル管理があります。各アクティビティは、onDestroyが呼び出されるまで「生きている」状態に保たれます。たとえば、OSはアクティビティを数時間存続させ、他のタスクを実行するのに十分なメモリがない場合にアクティビティを強制終了できます。

あなたの場合に起こることは、アプリを再度開いたときに同じアクティビティが再実行される可能性が高く(エミュレーターでは、アクティビティはおそらく以前に強制終了されます)、オブジェクトの一部が破棄または再実行されたため、状態が悪い状態になっています-初期化されました。

正しいことは、onPause / Resumeなどの他の状態コールバックのいくつかを使用して、アクティビティによって使用されるリソースを割り当て/破棄することです。

コードは次のようになります。

public class SomeActivity extends Activity
{
     public void onCreate()
     {
         super.onCreate();
         // Do some object initialization
         // You might assume that this code is called each time the activity runs.
         // THIS CODE WILL RUN ONLY ONCE UNTIL onDestroy is called.
         // The thing is that you don't know when onDestry is called even if you close the.
         // Use this method to initialize layouts, static objects, singletons, etc'.    
     }

     public void onDestroy()
     {
         super.onDestroy();
         // This code will be called when the activity is killed.
         // When will it be killed? you don't really know in most cases so the best thing to do 
         // is to assume you don't know when it be killed.
     }
}

コードは次のようになります。

public class SomeActivity extends Activity
{
     public void onCreate()
     {
         super.onCreate();
         // Initialize layouts
         // Initialize static stuff which you want to do only one time  
     }

     public void onDestroy()
     {
         // Release stuff you initialized in the onCreate
     }

     public void onResume()
     {
         // This method is called each time your activity is about to be shown to the user,        
         // either since you moved back from another another activity or since your app was re-
         // opened.
     }

     public void onPause()
     {
         // This method is called each time your activity is about to loss focus.
         // either since you moved to another activity or since the entire app goes to the 
         // background.
     }

}

結論:常に同じアクティビティを再実行できると想定してください。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ