java - 当前正在使用同一 Activity 时,从通知栏启动新 Activity

标签 java android android-intent android-pendingintent

我通过通知中的按钮启动特定的 Activity 。如果我当前没有打开该特定 Activity ,它将作为新 Activity 启动该 Activity 。但是,当我打开该特定 Activity 时,向下滚动通知然后单击该按钮,它不会执行任何操作,因为 Activity 已经打开。我需要将其作为新的打开,因为新值将传递到其中。我可以使用 SharedPreferences 来做到这一点,但我不想这样做。请帮忙

        intent = new Intent(context,AppReport.class);
        intent.putExtra("name",packageInstallName); //this does not get send over
        intent.putExtra("version",packageInstallVersion);
        PendingIntent testing = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        setOnClickPendingIntent(buttons[1],testing);

这就是从通知栏启动 Activity 的方式。我尝试使用 FLAG_UPDATE_CURRENT 作为标志,但也不起作用。

最佳答案

将您的 Activity 设置为 singleTop

<activity
        android:name=".AppReport"
        android:configChanges="screenSize|keyboard|orientation|screenSize"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden"/>

intent = new Intent(context,AppReport.class);
    intent.putExtra("name",packageInstallName); //this does not get send over
    intent.putExtra("version",packageInstallVersion);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent testing = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

重写 Activity 中的 onNewIntent(Intent Intent)

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //Do your stuff
}

关于java - 当前正在使用同一 Activity 时,从通知栏启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49114388/

相关文章:

android - 在最新的 Android Studio 版本 4.1.1 中,Genymotion 插件不再出现在 Android Studio 插件市场中

安卓/NFC : Get Tag in onCreate() without new Intent

java - Firestore删除集合中的所有对象

java - 登录到远程 mysql 数据库的身份验证

java - 使用 spring-security 基于 http 域进行身份验证

java - Android:StaggeredGridLayoutManager 中的 ArithmeticException

java - 为什么从 HashMap 返回的 String 与 System.out 相同但不匹配?

Android ConstraintLayout TextView 跳出屏幕

安卓切换 Activity

Android SQLite 将 gridview 中选择的 Item 传递给另一个 Activity 进行编辑