onNewIntent(intent)无法正常工作

Ame*_*ose 13 android oauth

我正在构建这个Twitter应用程序,其中调用浏览器以从我的主要活动进行身份验证..在线程中运行以下代码(AsyncTask)

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

这里

context is the context of main activity..the uri is returned through `intent`..
Run Code Online (Sandbox Code Playgroud)

在认证之后,它要做的是回到我的主要活动,到现在的状态.现在发生的是,它创建了另一个主要活动的实例.

我在用

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent); 
    SharedPreferences prefs = getSharedPreferences("twitter", 0);
    final Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) {

        setLoggedIn(true, tlogout);
        tstatus=true;

        new RetrieveAccessTokenTask(this,consumer,provider,prefs,tstring).execute(uri);

    }
}
Run Code Online (Sandbox Code Playgroud)

但仍然,问题仍然存在..另一件事是,一切正常模拟器(2.3)..不能在我的设备工作.. Android 2.3.6

我的清单是

<activity
    android:name=".mainActivity"

    android:launchMode="singleTask"

    android:label="@string/title_activity_main" >
    <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="x-oauthflow-twitter" android:host="callback" />

    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

Lui*_*uis 0

我不熟悉 Twitter 认证过程,但你可以尝试FLAG_ACTIVITY_CLEAR_TOP代替Intent.FLAG_ACTIVITY_SINGLE_TOP.

仅当您的主要活动位于堆栈顶部时,后者才适用于您,而另一个则清除堆栈并确保您的活动位于顶部。