当通过通知或外部意图打开应用时,Singletop Launchmode失败

发布于 2025-01-24 06:47:58 字数 3722 浏览 0 评论 0 原文

我有一个最小化的应用程序:

mainActivity.java


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onNewIntent(Intent intent){
        super.onNewIntent(intent);
    }

}

activityb.java

public class ActivityB extends AppCompatActivity {

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

        setContentView(R.layout.activity_b);
    }
}

androidmanifest.xml

        <activity
            android:name=".activities.MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme.NoActionBar">

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

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
                <data android:mimeType="audio/*" />
            </intent-filter>

        </activity>

        <activity
            android:name=".activities.ActivityB"
            android:exported="false"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>

1) i从使用TaskStackBuilder打开MainAttivity -&Gt; GT; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt; gt;正如文档所述():

        Intent resultIntent = new Intent(this, ActivityB.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
...
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "1")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText("Message")
                .setAutoCancel(true)
                .setContentIntent(resultPendingIntent);

        android.app.Notification buildNotification = mBuilder.build();
        NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(1, buildNotification);

通过通知单击打开ActivityB,但使用Back Butace不会转到Parent Activity(MainAttivity),这正是您使用TaskStackBuilder想要的。它使应用程序关闭。

2)当打开MainActivity的应用程序在后台(主页按钮)时,如果我共享另一个应用程序的音频文件到该应用程序,则始终称为onCreate而不是onewnewintent。从启动模式的文档中看,它不应该呼唤Onnewintent,因为MainAttivity是单旋转,并且位于堆栈的顶部?

中测试

  • Android 10&amp; Android 11
  • CompilesDkversion 32
  • TargetsDkversion 32

I have this minimized app:

MainActivity.java


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onNewIntent(Intent intent){
        super.onNewIntent(intent);
    }

}

ActivityB.java

public class ActivityB extends AppCompatActivity {

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

        setContentView(R.layout.activity_b);
    }
}

AndroidManifest.xml

        <activity
            android:name=".activities.MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme.NoActionBar">

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

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
                <data android:mimeType="audio/*" />
            </intent-filter>

        </activity>

        <activity
            android:name=".activities.ActivityB"
            android:exported="false"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>

1) I launch a notification from a Service with the app closed (killed) using TaskStackBuilder to open MainActivity -> ActivityB in the stack just as documentation says (https://developer.android.com/training/notify-user/navigation) :

        Intent resultIntent = new Intent(this, ActivityB.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
...
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "1")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText("Message")
                .setAutoCancel(true)
                .setContentIntent(resultPendingIntent);

        android.app.Notification buildNotification = mBuilder.build();
        NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(1, buildNotification);

ActivityB is opened on notification click but using back button doesn't go to parent Activity (MainActivity) which is precisely what you want with TaskStackBuilder. It gets the app closed.

2) When the app with MainActivity opened is in background (home button), if I share an audio file from another app to the app, onCreate is always called instead of onNewIntent. Looking at the documentation of launchMode, shouldn't it call to onNewIntent as MainActivity is singleTop and is in the top of the stack?

Tested in

  • Android 10 & Android 11
  • compileSdkVersion 32
  • targetSdkVersion 32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情何以堪。 2025-01-31 06:47:58

尝试SingleSinstance,似乎有另一个堆栈打开,否则系统确实会调用OnnewIntent而不是ongreate。

Try singleInstance, seems like there is another stack open otherwise the system would indeed call onNewIntent instead of onCreate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文