2016-03-10 25 views
1

我打开一个活动,从通知,打开罚款。 但是,我想打开它的父级活动,而我点击'后退按钮',目前它直接退出应用程序。我希望它导航到HomeScreenActivity。上传导航使用后台堆栈不起作用,同时点击通知

这是舱单申报 -

<activity 
     android:name="com.discover.activities.MyTrialsActivity" 
     android:exported="true" 
     android:parentActivityName="com.discover.activities.HomeScreenActivity" 
     android:screenOrientation="portrait"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.discover.activities.HomeScreenActivity" /> 
    </activity> 

这里是我的代码来生成通知 -

public static PendingIntent getAction(Activity context, int actionId) { 
    Intent intent; 
    PendingIntent pendingIntent; 

    intent = new Intent(context, MyTrialsActivity.class); 

    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack 
    //stackBuilder.addParentStack(HomeScreenActivity.class); 
    stackBuilder.addParentStack(HomeScreenActivity.class); 
    // Adds the Intent to the top of the stack 
    stackBuilder.addNextIntent(intent); 
    // Gets a PendingIntent containing the entire back stack 
    pendingIntent = 
      stackBuilder.getPendingIntent(0 /*request code */, PendingIntent.FLAG_ONE_SHOT); 

    /*pendingIntent = PendingIntent.getActivity(context, 0 *//* Request code *//*, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT*//*|PendingIntent.FLAG_ONE_SHOT*//*);*/ 
    return pendingIntent; 
} 

/** 
* Create and show a simple notification containing the message. 
* 
* @param message Message to show in notification 
*/ 
public static void sendNotification(Context context, String message, int actionId) { 
    PendingIntent pendingIntent = NotifUtils.getAction((Activity) context, actionId); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Title") 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setVibrate(new long[]{1000}) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
+0

从通知打开'ParentActivity'而不是打开'ChildActivity'并在意图中设置一个标志。在'ParentActivity'中,检查这个标志并开始ChildActivity。这样您就可以轻松导航回到家长活动。 –

+0

我有这个想法,但听起来不像解决方法,而android提供完整的方式来运送带有pendingIntent的后台堆栈? – Darpan

回答

0

我找到了解决Android的文档中

// Intent for the activity to open when user selects the notification 
Intent detailsIntent = new Intent(this, DetailsActivity.class); 

// Use TaskStackBuilder to build the back stack and get the PendingIntent 
PendingIntent pendingIntent = 
     TaskStackBuilder.create(this) 
         // add all of DetailsActivity's parents to the stack, 
         // followed by DetailsActivity itself 
         .addNextIntentWithParentStack(upIntent) 
         .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentIntent(pendingIntent); 

而且, Here是链接。

另请参阅this回答以获取更多参考。

+0

我已经完成了同样的事情,你在这里建议,所以这一个没有工作。 '.addNextIntentWithParentStack(parent)的顺序; .addNextIntentWithParentStack(childIntent)' 当我颠倒这个顺序时,我的通知是打开父项活动,并在后面按它打开子项活动。显然,这是我做错了什么,因为逆序工作,但不是真正的。 – Darpan

0

尝试使用startActivities(Context context, Intent[] intents)

Intent homeIntent = new Intent(context, HomeScreenActivity.class); 
     Intent newIntent = new Intent(context, MyTrialsActivity.class); 
     Intent[] intents = new Intent[]{homeIntent, newIntent}; 

     ContextCompat.startActivities(context, intents); 

因此,我们可以在同一时间启动多个活动,所以同时按下后退按钮将进入主页,而不是在quiting应用。

+0

PendingIntent也可以做到吗?因为我需要从通知中打开这些活动。 – Darpan

1

解决方案 -

我说我的孩子的活动以及在addParentStack(MyTrialActivity.class);
和它的工作如预期。 我认为加入addNextIntent()应该已经这样做了,尽管它没有那样工作..

相关问题