我通过通知中的按钮启动特定的 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/