1

PendingIntentを同じアクティビティで使用しようとしています。 MainActivityが既に実行されていて、アプリが通知を受け取った場合、この通知がクリックされるとTestFragmentクラスがロードされますが、ここでは該当しません。通知がクリックされても何も起こりません。PendingIntentは目的のフラグメントを起動しません

Intent intent = new Intent(mContext, MainActivity.class); 
    intent.putExtra("NOTIFICATION", "notify"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setAction(Long.toString(System.currentTimeMillis())); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new NotificationCompat.Builder(mContext) 
     .setContentIntent(pendingIntent) 
     .setContentTitle(title) 
     .setSmallIcon(R.drawable.logo) 
     .setLargeIcon(bitmap) 
     .setPriority(Notification.PRIORITY_MAX) 
     .build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(1, notification); 

しかし、私はちょうどIntentPendingIntentためにこのコードを使用する場合、私はそれが二回の活動を作成するしかしTestFragmentをロードするために取得します。デフォルトのビューの上にTestFragmentをロードします。 MainActivity.class に通知意図

Intent intent = new Intent(mContext, MainActivity.class); 
    intent.putExtra(NOTIFICATION, true); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = 
      PendingIntent.getActivity(mContext, 100, intent, 
         PendingIntent.FLAG_UPDATE_CURRENT); 

に余分

private static final String NOTIFICATION = "notification"; 

を渡す

Intent intent = new Intent(mContext, MainActivity.class); 
    intent.putExtra("NOTIFICATION", "notify"); 
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

MainActivity.classのonCreate()

String notificationIntent = getIntent().getStringExtra("NOTIFICATION"); 

    if(notificationIntent != null) { 
     if (notificationIntent.equals("notify")) { 
      TestFragment fr = new TestFragment(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
      fragmentTransaction.replace(R.id.frame_container, fr); 
      fragmentTransaction.commit();    
     } 
    } else { 
    //default view 
    } 
+0

何 'のように見えるonNewIntent'のでしょうか? – pskink

+0

@pskink onNewIntentをオーバーライドしませんでした – user9123

+1

** onNewIntent ** *これは、パッケージのlaunchModeを "singleTop"に設定するアクティビティ、またはstartActivity(Intent)を呼び出すときにクライアントがFLAG_ACTIVITY_SINGLE_TOPフラグを使用した場合に呼び出されます。いずれの場合も、アクティビティの新しいインスタンスではなく、アクティビティスタックの先頭にアクティビティが再起動されると、再起動に使用されたインテントを持つ既存のインスタンスに対してonNewIntent()が呼び出されますそれ* – pskink

答えて

1

定数は(onNewIntentを添加しました)

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    setIntent(intent); 
} 

とonResume()

@Override 
protected void onResume() { 
    super.onResume(); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     final boolean fromNotification = extras.getBoolean(NOTIFICATION); 
     if (fromNotification) { 
      TestFragment fr = new TestFragment(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
      fragmentTransaction.replace(R.id.frame_container, fr); 
      fragmentTransaction.commit(); 
     } 
    } 
} 

のAndroidManifest.xml

<activity 
    android:name="test.com.sample.MainActivity" 
    android:label="@string/app_name" 
    android:launchMode="singleTop"> 

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

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

    <meta-data 
     android:name="android.app.searchable" 
     android:resource="@xml/searchable" /> 
</activity> 
関連する問題