Android onNewIntent Uri始终为null

Android onNewIntent Uri始终为null,第1张

概述在我的activity的onNewIntent()方法中,getIntent().getData();永远是空的.在转到onCreate()或任何其他生命周期函数之前,肯定会使用此方法.它从浏览器返回此处,我不知道为什么getIntent().getData()虽然是null. 这个活动就像这个context.startActivity一样启动浏览器(new Intent(Intent.ACTIO 在我的activity的onNewIntent()方法中,getIntent().getData();永远是空的.在转到onCreate()或任何其他生命周期函数之前,肯定会使用此方法.它从浏览器返回此处,我不知道为什么getIntent().getData()虽然是null.

这个活动就像这个context.startActivity一样启动浏览器(new Intent(Intent.ACTION_VIEW,Uri.parse(requestToken.getAuthenticationURL())));

并返回此处

@OverrIDepublic voID onNewIntent(Intent intent){    super.onNewIntent(intent);    Uri uri = getIntent().getData();    if (uri != null && uri.toString().startsWith(TwitterConstants.CALLBACK_URL)) {...}}

但是uri总是空的.

明显的东西:

<activity        androID:name="myapp.mypackage.TweetFormActivity"        androID:configChanges="orIEntation|keyboardHIDden"        androID:label="@string/app_name"        androID:launchMode="singleInstance"        androID:screenorIEntation="portrait"        androID:theme="@androID:style/theme.Black.NoTitlebar">         <intent-filter>            <action androID:name="androID.intent.action.VIEW" />            <category androID:name="androID.intent.category.DEFAulT" />            <category androID:name="androID.intent.category.broWSABLE" />            <data androID:scheme="oauth" androID:host="myapp"/>        </intent-filter>        </activity>static final String CALLBACK_URL = "oauth://myapp";

我在这里想念的是什么?谢谢

解决方法 您应该在获取URI之前为intent参数调用getData()或执行setIntent(intent). onNewIntent()不会自动设置新意图.

更新:所以,这里有两种方法可以实现onNewIntent().第一个用新的意图替换旧意图,所以当你稍后调用getIntent()时,你将收到新意图.

@OverrIDeprotected voID onNewIntent(final Intent intent) {    super.onNewIntent(intent);    // Here we're replacing the old intent with the new one.    setIntent(intent);    // Now we can call getIntent() and receive the new intent.    final Uri uri = getIntent().getData();    // Do something with the URI...}

第二种方法是使用来自新意图的数据,保留旧的意图.

@OverrIDeprotected voID onNewIntent(final Intent intent) {    super.onNewIntent(intent);    // We do not call setIntent() with the new intent,// so we have to retrIEve URI from the intent argument.    final Uri uri = intent.getData();    // Do something with the URI...}

当然,您可以使用两种变体的组合,但不要期望从getIntent()接收新意图,直到您使用setIntent()显式设置它.

总结

以上是内存溢出为你收集整理的Android onNewIntent Uri始终为null全部内容,希望文章能够帮你解决Android onNewIntent Uri始终为null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1122901.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存