我看到了几种方法,我尝试了一切,但无法使它工作.我不知道为什么它如此复杂,在文档中它看起来如此简单!我想通过通知触发OnNewIntent(用户在通知栏中单击它).
目前我已将Activity设置为singleTop
<activity
android:name="-.-.-.MainMenuActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
</activity>
Run Code Online (Sandbox Code Playgroud)
这是我创建通知的代码:
public void showNotification(String text, String componentId){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainMenuActivity.class);
if(!componentId.equalsIgnoreCase("")){
resultIntent.putExtra("componentId", componentId);
}
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
mBuilder.setFullScreenIntent(resultPendingIntent, false);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
这是我的MainMenuActivity中的OnNewIntent方法:
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 我在我的活动onCreate()中初始化我的列表,如下所示:
private List<MyItem> filtered;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
filtered = new ArrayList<>();
// more things
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用onNewIntent中的过滤项时,有时我会得到一个空指针异常.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
filtered.clear();
}
Run Code Online (Sandbox Code Playgroud)
怎么可能呢?
编辑:我的Activity的launchmode是SingleTask
编辑2:
我无法发送更多有用的日志,因为此崩溃正在生产中.只有我得到一些织物日志.
感谢您的帮助,但我无法粘贴整个代码隐私的原因.
我认为我在SingleTask-OnCreate-OnNewIntent使用方面存在问题.我只是试图通过通知打开我的应用程序,并通过参数决定当用户导航到活动时将打开哪个片段.
你有任何关于这个包含SingleTask-OnCreate-OnNewIntent实现的例子吗?
感谢大家的帮助.
看看这个问题: onNewIntent()生命周期和注册的监听器
我看到OP遇到了同样的问题,但我不清楚为什么提供的解决方案符合他的目的.
当用户点击他们的NFC到设备时,我正在从传入的Intent收集信息,但是,当我收集这些数据时,我不希望onPause或onResume触发.如果没有办法阻止这种情况发生,除了通过监听onNewIntent之外,还有另一种方法可以收集传入的NFC信息吗?我在onResume中有一堆逻辑,当用户没有离开应用程序时,完全没有准备好突然处理被叫出来...我希望避免重构我的代码来处理这个意外偶然性.提前致谢.