【问题标题】:Up to parent activity - on Android取决于父活动 - 在 Android 上
【发布时间】:2020-11-19 18:32:51
【问题描述】:

在我的应用中收到通知后,点击它会打开活动 B。活动 B 有一个父活动 A。这是清单:

<activity
    android:name="com.evapp.activities.B"
    android:label="@string/title_activity_B"
    android:parentActivityName="com.evapp.activities.A"
    android:screenOrientation="portrait" >

    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.evapp.activities.A" />
</activity>

在活动 B 中,我启用了向上功能(活动操作栏图标附近的左箭头),代码如下:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    ...

问题是,如果通过单击通知打开了活动 B(活动 A 不是带来活动 B 的活动),那么当单击图标时,应用程序将关闭。我想让它打开它的父活动,A. 有可能吗?还是我应该使用活动 B 中的 startActivity() 来做?

更新 1- 我已添加此代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
   switch (item.getItemId()) 
   {
       case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent))
            {
                TaskStackBuilder.create(this)
                    .addNextIntentWithParentStack(upIntent)
                    .startActivities();
            } 
            else 
            {
                NavUtils.navigateUpTo(this, upIntent);
            }

            return true;

谢谢

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    您需要设置用于构建NotificationPendingIntent,以启动新任务,并为PendingIntent提供back stack以实现应用程序的正常Up行为。

    Intent resultIntent = new Intent(this, SecondActivity.class);
    
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // All the parents of SecondActivity will be added to task stack.
    stackBuilder.addParentStack(SecondActivity.class);
    // Add a SecondActivity intent to the task stack.
    stackBuilder.addNextIntent(resultIntent);
    
    // Obtain a PendingIntent for launching the task constructed by this builder.
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(REQUEST_CODE, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(this)
        .setContentTitle("My Notification")
        .setContentText("Notification content")
        .setSmallIcon(android.R.drawable.ic_menu_view)
        .setContentIntent(pendingIntent)
        .build();
    
    manager.notify(NOTIFICATION_ID, notification);
    

    请阅读 Preserving Navigation when Starting an Activity 上的 Android 官方文档。它推荐上述方法。

    【讨论】:

    • 在使用TaskStackBuilder时,不要忘记在Manifest中添加Parent activity声明。
    • 另外不要忘记使用 if(NavUtils.getParentActivityName( this ) != null ) NavUtils.navigateUpFromSameTask( this );在 Activity 中的 onBackPressed() 中,或者在从片段完成 Activity 时。如果 if 语句失败,则使用 super.onBackPressed 或 finish()。
    • 它似乎不适用于 Android 7.0 (Nexus 5X)。有什么想法吗?
    • 如果 A 为结果启动 B,而这次 B 从 Notification 开始,我们如何将 B 的更改传递给 A?
    【解决方案2】:

    我使用下面的代码,它就像一个魅力。试一试!

    Intent upIntent = new Intent(getApplicationContext(), Home.class);
    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
        Log.d("ShowNotifications", "New Home");
        TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
    } else {
        Log.d("ShowNotifications", "Old Home");
        upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //upIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
        startActivity(upIntent);
        finish();
    }
    

    【讨论】:

      【解决方案3】:

      创建一个临时活动以在单击通知时启动(不使用 setContentView)。在那里,您可以决定要启动哪个活动。取决于你的逻辑。

      【讨论】:

        【解决方案4】:

        直到父活动 - 在 Android 上

        您可以简单地使用一些布尔参数启动活动父活动,如果在活动 b 中设置了布尔参数,则启动活动 A。这样两个活动都将在堆栈上运行或使用 taskstackbuilder。

        【讨论】:

          【解决方案5】:

          使用TaskStackBuilder时,不要忘记在Manifest中添加Parent activity声明。

          对于 API 16 +:

          <activity android:name=".SecondActivity"
          android:parentActivityName=".MainActivity"
          ... />
          

          对于 API

          <activity android:name=".SecondActivity">
          <meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value=".MainActivity" />
          

          【讨论】:

            【解决方案6】:

            如果@Manish Mulimani 的解决方案对您有用,请随时忽略此信息。它对我不起作用,我还没有尝试过 OP 中的代码,但如果这些都不适合你,这里有一个替代解决方案:

            首先,和 OP 一样,在 AndroidManifest.xml 中指定父 Activity:

            <activity
                android:name=".DetailActivity"
                android:label="@string/activity_detail"
                android:parentActivityName=".MainActivity" >
            
                <!-- Parent activity meta-data to support 4.0 and lower -->
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".MainActivity" />
            </activity>
            

            然后,在您编写的用于显示通知的代码中,执行以下操作:

            /* The Intent you want to open when your notification is clicked */
            Intent detailIntent = new Intent(context, DetailActivity.class);
            /* If you have some extras or a URI, set it here for the DetailActivity */
            detailIntent.setData(todaysWeatherUri);
            
            /*
             * This Intent is the one that goes back to your MainActivity when the up 
             * arrow or back button is clicked from the DetailActivity. 
             */
            Intent backToMain = new Intent(context, MainActivity.class);
            
            PendingIntent resultPendingIntent =
                    TaskStackBuilder.create(context)
                            .addNextIntentWithParentStack(backToMain)
                            .addNextIntent(detailIntent)
                            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);   
            

            我已经在每次启动活动的迭代中测试了这一点,我可以精简它并且它运行良好!我希望它对你有帮助。

            Here is the link for the Android Documentation from where I formed this solution.

            【讨论】:

              【解决方案7】:
              <activity
                  android:name=".SecondActivity"
                  android:parentActivityName=".ParentActivity"
                  android:screenOrientation="portrait"/>
              
              val secondIntent = Intent(this, SecondActivity::class.java)
              val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
                       addNextIntentWithParentStack(secondIntent)
                       getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
                  }
              

              TaskStackBuilder parentStack 解决方案仅适用于 Android &lt; 7. 对于 Android 7+ 我认为您应该将 parentActivity 和 secondActivity 添加到 TaskStack强>。

              val parentIntent = Intent(this, ParentActivity::class.java)
              val secondIntent = Intent(this, SecondIntent::class.java)
                  val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
                      addNextIntent(parentIntent)
                      addNextIntent(secondIntent)
                      getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
                  }
              

              【讨论】:

                猜你喜欢
                • 2013-12-17
                • 1970-01-01
                • 1970-01-01
                • 2014-06-27
                • 2013-03-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-09-23
                相关资源
                最近更新 更多