Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从通知打开应用程序时如何应用正常的活动顺序_Android_Android Intent_Android Activity_Android Notifications_Android Navigation - Fatal编程技术网

Android 从通知打开应用程序时如何应用正常的活动顺序

Android 从通知打开应用程序时如何应用正常的活动顺序,android,android-intent,android-activity,android-notifications,android-navigation,Android,Android Intent,Android Activity,Android Notifications,Android Navigation,下面是我的场景:我有一个服务,可以播放流式音乐并创建(和更新)通知。按下通知时,用户将进入B活动 我的应用程序的结构是A->B 当用户通过以下流程进入主屏幕:B->A->主屏幕(服务继续播放音乐),按下通知将用户带到活动B,但现在他无法返回活动A。他将被带到主屏幕 我需要为每种情况执行B->A命令 以下是我的代码片段: AndroidManifest.xml: <activity android:name=".ChannelListActivity" android:con

下面是我的场景:我有一个服务,可以播放流式音乐并创建(和更新)通知。按下通知时,用户将进入B活动

我的应用程序的结构是A->B

当用户通过以下流程进入主屏幕:B->A->主屏幕(服务继续播放音乐),按下通知将用户带到活动B,但现在他无法返回活动A。他将被带到主屏幕

我需要为每种情况执行B->A命令

以下是我的代码片段:

AndroidManifest.xml:

<activity
    android:name=".ChannelListActivity"
    android:configChanges="locale|keyboard|keyboardHidden|screenLayout|fontScale|uiMode|orientation|screenSize"
    android:launchMode="singleTask" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name=".NowPlayingActivity"
    android:parentActivityName=".ChannelListActivity"
    android:launchMode="singleTop" >
</activity>
那么,我需要改变或增加什么来实现这种行为呢?先谢谢你


编辑:我试图添加以下代码,但仍然没有得到想要的结果

Intent intentGoToApp = new Intent(this, NowPlayingActivity.class);
intentGoToApp.putExtra(NowPlayingFragment.EXTRA_CHANNEL_ID, mUserData.getPlayingChannelId());
intentGoToApp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//PendingIntent piGoToApp = PendingIntent.getActivity(getApplicationContext(), 0, intentGoToApp, PendingIntent.FLAG_CANCEL_CURRENT);

Intent intentMainActivity = new Intent(this, ChannelListActivity.class);

PendingIntent piGoToApp = TaskStackBuilder.create(this)
// add all of DetailsActivity's parents to the stack,
// followed by DetailsActivity itself
    .addNextIntentWithParentStack(intentMainActivity)
    .addNextIntentWithParentStack(intentGoToApp)
    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);*/

您需要将活动A添加到您的应用程序中,以便Android知道退出每个活动的顺序

你可能会发现这很有用

从Android 4.1(API级别16)开始,您可以声明逻辑 通过指定
android:parentActivityName
属性,该属性位于
元素中。这使系统能够 简化导航模式,因为它可以确定逻辑 使用此信息备份或备份导航路径


通过在AndroidManifest.xml文件中声明此关系,使活动A成为活动B的父级,因此每次按下“后退”按钮时,将调用父级活动:

<activity
    android:name=".ActivityB"
    android:label="Activity B"
    android:parentActivityName=".ActivityA">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.yourpackage.MainActivity" />
</activity>

您可以使用以下技巧:

  • 在活动B的
    public void onBackPressed(){}
    中启动活动A,前提是活动B已通过通知启动(并完成活动B)

  • 不是通过通知启动活动B,而是启动活动A来处理意图并立即启动活动B

    public class ActivityB extends Activity {
        static final String ACTION_NOTIFICATION = "ACTION_NOTIFICATION";
        boolean isStartedWithNotification;
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ....
        }
    
        // OR onResume() if you don't use fragments
        @Override
        protected void onResumeFragments() {
            super.onResumeFragments();
            handleIntent();
        }
    
        void handleIntent() {
            Intent intent = getIntent();
            if (intent != null && ACTION_NOTIFICATION.equals(intent.getAction())) {
                // handle intent, populate UI based on it's information;
                isStartedWithNotification = true;
                setIntent(null);
            }
        }
    
        @Override
        public void onBackPressed() {
            if (isStartedWithNotification) {
                startActivity(new Intent(this, ActivityA.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                finish();
            } else {
                super.onBackPressed();
            }
        }   
    }
    
    public class ActivityA extends Activity {
        static final String ACTION_NOTIFICATION = "ACTION_NOTIFICATION";
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
        }
    
        // OR onResume() if you don't use fragments
        @Override
        protected void onResumeFragments() {
            super.onResumeFragments();
            handleIntent();
        }
    
        void handleIntent() {
            Intent intent = getIntent();
            if (intent != null && ACTION_NOTIFICATION.equals(intent.getAction())) {
                setIntent(null);
                //START ACTIVITY B
            }
        }
    }
    
  • 第一种方法更具黑客性,但使用第一种方法,在低端设备上,您可能会看到活动切换的“闪烁”。所以我从第一个开始


    我希望,这会有所帮助

    谢谢Knossos,但在将活动A添加到backbackback之后,它仍然不起作用。请在问题的编辑部分修改代码。谢谢你,康斯坦丁,你的建议真的帮助了我。
    public class ActivityA extends Activity {
        static final String ACTION_NOTIFICATION = "ACTION_NOTIFICATION";
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
        }
    
        // OR onResume() if you don't use fragments
        @Override
        protected void onResumeFragments() {
            super.onResumeFragments();
            handleIntent();
        }
    
        void handleIntent() {
            Intent intent = getIntent();
            if (intent != null && ACTION_NOTIFICATION.equals(intent.getAction())) {
                setIntent(null);
                //START ACTIVITY B
            }
        }
    }