2016-03-30 24 views
2

私のアプリでは、ユーザーがアプリ通知をクリックしたときに起動するActivityがあります。ユーザーが通知をクリックしたときに通知からアクティビティを開始すると、MainActivityも開始されます

時には、Activityが正しく表示されますが、時々アプリのためのMainActivityも戻って正しいActivity開始され、ユーザーが戻って正しいActivityでクリックしたときに表示されます。私は AlertActionReceiverActivity"android:launchMode=singleTask"を追加しようとしている

は、それが通知から起動せず、また、全くそれを追加してActivit Yであるが、結果は同じです。

この問題を解決する運がありますか?

<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<activity android:name=".AlertActionReceiverActivity" android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent" /> 
+0

を参照してください。助けを願ってい

(単なるAPIの互換性!!に注意してください)この[post](http://stackoverflow.com/questions/10184351/how-to-start-activity-when-user-clicks-a-notification)に連絡し、あなたに役立つかどうかを確認してください – Vucko

+0

ここに通知コードを記入してください。 –

答えて

1

あなたが戻ったときにAndroid上で、デフォルトでは、それは以前のものが表示される親アクティビティではなく、ので、それはあります。

溶液はAndroidのドキュメントで説明されている:それは特別なフラグでpendingIntentを使用することです: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

私はこれが

+0

NotificationActivityにtaskAffinity = ""修飾子を、通知フラグにIntent.FLAG_ACTIVITY_CLEAR_TASKを追加した後に正常に動作しているようです。ありがとう! –

0
<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".AlertActionReceiverActivity" 
     android:parentActivityName=".ABCActivity" 
     android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".ABCActivity"/> 
    </activity> 

また、適切TaskStackを作成します。

Intent resultIntent = new Intent(this, ResultActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(ResultActivity.class); 
    PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    stackBuilder.addNextIntent(resultIntent); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

    Intent notifyIntent = 
     new Intent(new ComponentName(this, ResultActivity.class)); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
     Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    PendingIntent notifyPIntent = 
     PendingIntent.getActivity(
     this, 
     0, 
     notifyPIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT 
    ); 
    builder.setContentIntent(notifyPIntent); 
    NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(id, builder.build()); 
+0

この例はありがたいですが、私がそれに従うことができるかどうかはわかりません... IntentとPendingIntentに同じ変数名を使用していて、なぜTaskStackBuilderを定義するのか分かりませんそれを定義した後、あなたは通知に全く割り当てません。とにかくありがとう! –

+0

@ TheMatrixさて、あなたはコードを提供していないので、自分で変数を取っただけです。しかし、これがとにかく役に立つなら、あなたはアップヴォートすることができます。 –

関連する問題