2016-07-26 3 views
1

私のアプリケーションには、私のアプリケーションでさまざまなアクティビティ間で移行するために使用するいくつかの "インテント"があります。私はSamsungデバイスで発生する奇妙な動作に気づきましたが、Nexusデバイスでは発生しませんでした。新しいインテントが作成されるたびに、この新しいアクティビティの2番目の「タスク」が起動されます。ユーザーがマルチタスクメニューに行くと、アプリケーションの複数のコピーを見ることができます!これは望ましい動作ではありません。どんなアドバイスも大歓迎です!すべてのインテントは、Android Appで新しいタスクを開始します。

マニフェスト:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" 
    android:launchMode="singleInstance"> 
    <activity 
     android:name=".MainActivity" 
     android:screenOrientation="portrait" 
     android:launchMode="singleInstance"> 
    </activity> 
    <activity 
     android:name=".Settings_area" 
     android:screenOrientation="portrait" /> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps" /> 
    <activity android:name=".Splash" 
     android:launchMode="singleInstance"> 


     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".aboutPageActivity" /> 
    <activity android:name=".turnOffFromNotification" 
     android:noHistory="true"></activity> 
</application> 

私はすでに起動モードを削除するだけでなく、singleTopstandardにアプリケーション起動モードを変更を試みてきました。

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       new Handler().postDelayed(new Runnable(){ 
        @Override 
        public void run() { 
      /* Create an Intent that will start the Menu-Activity. */ 
         Intent mainIntent = new Intent(Splash.this,MainActivity.class); 
         Splash.this.startActivity(mainIntent); 
         Splash.this.finish(); 
        } 
       }, splashDisplayLength); 
       return; 
      } 

意図第三のインスタンスを作成する:2番目のインスタンスを作成

意図インスタンスは起動から作成することができる

public void goToAboutPage() 
{ 
    Intent goToAboutPage = new Intent(this, aboutPageActivity.class); //create the intent to go to the map screen 
    startActivity(goToAboutPage); //actually go to the map screen 
} 

設定の意図:

public void changeToSettingsScreen() //changes the screen to the setting screen 
{ 
    readyToSendPackets = false; 
    sendSwitch.setChecked(false); 
    // textView.setText("NOT sending"); //set the textview to advise users packets are not being sent 
    Intent goToSettings = new Intent(this, Settings_area.class); 
    startActivity(goToSettings); 
} 

私はまた、オーバーonNewIntent方法を乗っ:

protected void onNewIntent(Intent intent) { 
    // super.onNewIntent(intent); //REMOVED THIS TO AVOID DOUBLE INSTANTIATION ON TOUCHWIZ IF ANYTHING BREAKS LOOK HERE FIRST 
    setIntent(intent); //this allows us to recieve the extras bundled with the intent 
    // System.out.println("Here is the bindle: " + getIntent().getExtras()); 
    if (getIntent().getExtras() != null) //check to see if there are any extras, there wont be on apps first start 
    { 
     Bundle extras = getIntent().getExtras(); //get the extras 
     String methodName = extras.getString("methodName"); //assign the extras to local variables 

     if(methodName != null && methodName.equals("turn_send_switch_off")) 
     { 
      sendSwitch.setChecked(false); 
     } 
     //else if(**other actions that may need to be performed can go here**) 
    } 

は、任意の助けをありがとうございました!!!

+0

singleInstanceのみをアプリケーションに追加しようとしましたか? – MiltoxBeyond

+0

なぜsingleInstance起動モードが必要ですか? – Shaishav

+0

もう1つのオプションは、新しいフラグメントライフサイクルに移行することです。フラグメントは1つのアクティビティに置き換えて追加することができます – MiltoxBeyond

答えて

1

通常、アプリのインスタンスを1つ強制する必要がある場合は、アクティビティごとにインスタンスを起動しようとするので、各アクティビティにandroid:launchMode="singleInstance"を入れないでください。

launchModeをアプリケーション以外のすべてから削除すると、@ Shaishavが言っていることはありますが、ほとんどの場合、アンドロイドにはアプリケーションのライフサイクルを処理させることができます一度に1つのインスタンスしか実行されないようにする必要がある場合を除き、launchModeとなります。

+0

'android :launchMode'は ''タグの有効な属性ではありません。これは無視され、役に立たず、おそらくあなたのコードを維持する必要がある誰かを混乱させるでしょう。それを除く。 –

+0

これは受け入れられる回答ですか?本当に? –

関連する問題