2

First of all i give you a short example for my problem. Keep in mind that the wanted behavior of my app is for my own use only.

Lets say i have an app with one activity and some fragments. This app starts up through a static broadcastreceiver with an intent e.g. ACTION_POWER_CONNECTED. To this point its all fine with my implementation.

But theres a missbehavior, which i dont know how to solve right.

If the app is running and i plug my usb off and on again, a new second instance of my activity will be created with the first one still running. Just to make it clear: If i press the back button the second one disappears and the first instance shows his UI.

Im searching a way to restart my app if its already running and to start it if its not running. There is no need for saving data for any kind of reuse.

I tried some combinations with the flags FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_BROUGHT_TO_FRONT and the FLAG_ACTIVITY_CLEAR_TOP. But i think these are the wrong ones for my case. Next try was to set the Activity in the manifast to singleInstance to be sure there can be no second instance. With no great success at all.

Can anyone give me an example to solve my problem like which flags i have to use?

4

2 に答える 2

2

マニフェスト ファイルのアクティビティ タグにandroid:launchMode="singleTask"を設定して、もう一度お試しください...

アクティビティが既に実行されている場合は、上記のタグを使用して新しいアクティビティを作成しないでください。古い実行中のアクティビティを表示します。

古い実行中のアクティビティでUIまたはデータを更新する必要があり、次にインテントブロードキャスト時に呼び出されるonResumeメソッドを使用します...

ありがとう

于 2012-09-13T10:08:50.750 に答える
1

(マニフェストで) アクティビティの launchMode 属性を使用する必要があります。

詳細については、 singleTop を使用するか、http://developer.android.com/guide/topics/manifest/activity-element.html#lmode を参照てください

于 2012-09-13T10:09:48.650 に答える