Reputation: 1699
I am implementing the OAuth part of some twitter work but am having trouble getting the callback where I expect. I send the user off to the browser to authorise the application, but the callback starts a new activity rather than returning to the activity that sent it. From the emulator, the back stack looks like this:
TwitterActivity --> Rather than this starting, I want to return to the original one
BrowserActivity
TwitterActivity --> This sends the request to the browser
I have singleTop set for the activity:
<activity
android:name=".TwitterSearchActivity"
android:theme="@style/Theme.Sherlock.Light"
android:configChanges="orientation"
android:launchMode="singleTop" >
<!-- Used for OAuth callback -->
<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:host="callback"
android:scheme="twitter-search-callback" />
</intent-filter>
</activity>
So I would expect that instead of getting a new activity created, that the original one would receive the onNewIntent(), but it is not happening.
Here is the code to start the browser in case it is important:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(token.getAuthenticationURL()));
startActivity(intent);
Upvotes: 2
Views: 584
Reputation: 1699
In the end, what I did was instead of sending it out to the browser, I created my own WebView activity and can then launch the intent from there. On the webview itself I have this client:
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(mScheme)) {
finish();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
view.loadUrl(url);
return true;
}
...
Note that it is key that finish() is called before startIntent so that the activity stack then has the launching activity (TwitterActivity in the original question) on top and in this case, being flagged as singleTop, it will be brought up again and receive the intent via the onNewIntent method.
Upvotes: 2