【问题标题】:Going back to main activity after launching activity from notification从通知启动活动后返回主要活动
【发布时间】:2017-07-12 10:50:43
【问题描述】:

我从收到的notification 启动activity。如果按下notification,它将启动activity。如果back-stack 上没有以前的activities,或者只有某个,我想删除那个特定的activity 并在其中插入我的主要activity,而不是新活动。

我找到了这个Thread,但我不明白他是如何用两个意图和标志来处理它的。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)

按照他的方式做是明智的,还是我应该为此编辑活动堆栈?

我对 android dev 还很陌生,所以一些建议可以帮助我。 非常感谢 ;)

更新:所以我使用了 stackbuilder 但不知何故它没有设置正确......我没有找到我的错误,我的布尔 noActivity 肯定会设置,但我认为我误解了堆栈实际上是如何放置的以前的活动。

private void sendNotification(messageType type, Map<String, String> data, boolean noActivities) {
    Intent i;
    String message = "";

    switch (type) {
        case newFOLLOWER:
            User cur = new User(data.get("other.name"));
            User.lookAT = User.getOtherUserByName(cur);
            i = new Intent(this, other_profile.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            message = data.get("other.name") + " is following you now. Click to see his profile";
            i.putExtra("Notification", data.get("other.name") + " is following you now. Click to see his profile");
            break;

        default:
            i = null;
            break;
    }

    if (i != null) {
        TaskStackBuilder stack = TaskStackBuilder.create(this);

        if(noActivities){
            stack.addParentStack(Photostream.class);
        }

        stack.addNextIntent(i);

        PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("PIC LOC")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }

}

更新 2:所以经过大量搜索后,我发现我误解了堆栈构建器的工作原理。我找到了另一个线程,他们在其中描述了添加的工作原理。 Editing the Manifest in order to have a previous stack。 我要快速跳过你提供给我的部分教程......

感谢你们的帮助;)

【问题讨论】:

    标签: android android-intent android-notifications


    【解决方案1】:

    您应该在创建这样的通知时创建stack builder

    Intent resultIntent = new Intent(this, NotificationTapActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
     // Adds the back stack
    stackBuilder.addParentStack(MainActivity.class);
     // Adds the Intent to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
    PendingIntent.FLAG_UPDATE_CURRENT);
    

    所以每当用户点击通知时,MainActivity 都会被插入到堆栈中。

    另一种解决方案,您可以在其中处理 NotoificationTapActivity 的后按。您可以在其中检查堆栈中是否存在注释,然后您可以完成当前活动并从那里启动 MainActivity。

    【讨论】:

      【解决方案2】:
      /**Just use below code inside onMessageReceived()**/
      
       PendingIntent pendingIntent;
          Uri defaultSoundUri;
          Intent notification_intent;
      
       String message = "Your Message";
      
       notification_intent = new Intent(this, YourActivity.class);
       notification_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      
       pendingIntent = PendingIntent.getActivity(this, 0 , notification_intent, PendingIntent.FLAG_ONE_SHOT);
              defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      
              notificationBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(getNotificationIcon(notificationBuilder))
                      .setContentTitle(this.getResources().getString(R.string.app_name))
                      .setContentText(message)
                      .setAutoCancel(true)
                      .setSound(defaultSoundUri)
                      .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                      .setContentIntent(pendingIntent);
      
         String[] multilineDescription = new String[]{message};
      
          message = multilineDescription[0];
      
          for (int i=1; i<multilineDescription.length; i++)
          {
              message += System.getProperty("line.separator");
              message += multilineDescription[i];
          }
      
          notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
      
          /***Above commented is for strecting the big message in Push Notification******/
      
          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          notificationManager.notify((int)MESSAGE_NOTIFICATION_ID  /* ID of notification */, notificationBuilder.build());
      

      【讨论】:

        【解决方案3】:

        您应该使用TaskStackBuilder。这是最有效的方式,TaskStackBuilder 结构正是为此而开发的。

        当我的项目中包含Push Notifications 时,我总是使用TaskStackBuilder

        覆盖onBackPress() 不是一个好方法,因为它需要在您的应用程序中使用复杂的结构,并且您需要处理很多事情。

        查看this教程。

        【讨论】:

          【解决方案4】:

          您可以在通知活动的 onBackPressed 中简单地处理它。

          在 flag 的帮助下,您可能知道您的通知活动是通过通知单击打开的,然后在 onBackPressed 中转到您的 mainactivity,如下所示:

          Intent intent =new Intent(context,MainActivity.class) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

          【讨论】:

            【解决方案5】:

            PendingIntent 可以实现多意图。只需创建一个数组意图

            Intent[] intents = new Intent[2];
                Intent intent1 = new Intent(this, MainActivity.class);
                Intent intent2 = new Intent(this, YourActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, intents,PendingIntent.FLAG_ONE_SHOT);
            

            当你完成 YouActivity 后,它会回到 MainActivity。

            【讨论】:

              【解决方案6】:
              Intent intent = getIntent();                   
              String data= intent.getStringExtra("from");                                             
              if(data.equalsIgnoreCase("notificaton")                                           
              {
              
               // call Mainactivity
              
               }                                                                              
               else if(){} 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-02-08
                • 2013-02-17
                • 2016-09-20
                • 2013-01-20
                • 2020-02-23
                相关资源
                最近更新 更多