本文介绍了Android的开放的onNewIntent始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动的onNewIntent()方法, getIntent()的getData(); 总是空。这肯定去这个方法才去的onCreate()或任何其他生命周期的功能。它返回这里从浏览器,我不知道为什么 getIntent()的getData()为空,但。

In my activity's onNewIntent() method, getIntent().getData(); is always null. It definitely goes to this method before going to onCreate() or any other lifecycle function. It returns here from a browser, I don't know why getIntent().getData() is null though.

本活动开始这样的浏览器 context.startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(requestToken.getAuthenticationURL())));

this activity starts the browser like this context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));

和返回此处

@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent);

    Uri uri = getIntent().getData();
    if (uri != null && uri.toString().startsWith(TwitterConstants.CALLBACK_URL)) {...}
}

但URI总是空。

but uri is always null.

清单的东西:

  <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";

我缺少什么吗?谢谢

what am I missing here? thanks

推荐答案

您应该叫的getData()意图参数或获得URI之前执行 setIntent(意向) onNewIntent()不会自动设置新的意图。

You should call getData() for the intent argument or perform setIntent(intent) before obtaining URI. onNewIntent() doesn't set new intent automatically.

更新:所以,here're两块可以实现途径 onNewIntent()。第一个替换旧意图用新的,所以当你调用 getIntent()后,您将收到新的意图。

UPDATE: So, here're two ways that you can implement onNewIntent(). The first replaces the old intent with the new one, so when you call getIntent() later, you will receive the new intent.

@Override
protected 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...
}

第二种方法是使用数据的新意图离开旧原样。

The second way is to use data from the new intent leave the old one as-is.

@Override
protected 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()

Of course, you can use a combination of two variants, but do not expect to receive the new intent from getIntent() until you explicitly set it with setIntent().

这篇关于Android的开放的onNewIntent始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:38