当前正在使用相同的 activity 时,从通知栏启动新的 activity
Launch a new activity from notification bar when that same activity is currently being used
我通过通知中的按钮启动特定的 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
}
我通过通知中的按钮启动特定的 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
}