I use Twitter4J to connect to Twitter. I prevent loading through the default browser by setting WebViewClient for WebView. The problem is Twitter doesn't return to my Activity. It shows "Web page not available" on WebView after redirecting.
Calling
final RequestToken requestToken = twitter.getOAuthRequestToken(Constant.CALLBACK_URL);
twitterSite.loadUrl(requestToken.getAuthenticationURL());
onNewIntent & WebViewClient
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
setResult(RESULT_OK);
finish();
}
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.startsWith("http")) {
view.loadUrl(url);
return true;
}
return false;
}
Manifest
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TwitterLoginActivity"
android:label="@string/title_activity_main"
android:launchMode="singleInstance">
<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="test123"/>
</intent-filter>
</activity>
</application>
CALLBACK_URL
public static final String CALLBACK_URL = "test123:///";