[英]Bundle is null when starting an activity from a listener using intent
当我使用此代码时,捆绑包是 null。 Logcat 显示:
onSuccess: starting activity with intent
getTokenFromIntent: bundle IS null (true)
MyListenerService.java
Bundle bundle = new Bundle();
bundle.putString("Name",message.getName());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(bundle);
Log.d(TAG, "onSuccess: starting activity with intent");
startActivity(intent);
MainActivity.java
TokenMessage token = null;
Intent intent = getIntent();
if (intent!=null) {
Bundle bundle = intent.getExtras();
if (bundle!=null) {
token = new TokenMessage();
token.setName(bundle.getString("Name"));
} else {
Log.d(TAG, "getTokenFromIntent: bundle IS null (" + (bundle==null) + ")");
}
} else {
Log.d(TAG, "getTokenFromIntent: intent IS null (" + (intent==null) + ")");
}
我有这个在另一个项目的类似模块中工作,并且一直在比较它们。 唯一显着的区别是工作的 MainActivity 扩展了 Activity,而这个扩展了 AppCompatActivity。
我看了这个,但代码似乎是一致的,因为它确实使用了一个包。 我也对此进行了审查,但我的是基本的,并且确实传递了所展示的值。 我回顾了这个,但它没有正式的答案,但我有一个例子,我使用了一个可以工作的包,许多例子也是如此。 这个没有被接受的答案。
我肯定错过了什么。 也许这是一个愚蠢的小错误,但它让我有点发疯。
当我们从 Service 或使用 singleTask 标志启动活动时,应从 onNewIntent 方法检索 Intent。
使用以下链接了解更多详情。 https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.