0

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:///";
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139
  • Answer of http://stackoverflow.com/questions/8306237/android-login-with-twitter-using-twitter4j might help~ – Hiral Vadodaria Aug 02 '12 at 06:29
  • long..back... but am writing..... this would may help others you just check the webView.canGoBack() return true or false. Based on that you can write the code. – GvSharma Nov 14 '14 at 09:39

2 Answers2

1

your code is fine. just need to changes like below

 private class CustomWebViewClient extends WebViewClient 
     {
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url)     
        {         
            if(url.startsWith(Constant.CALLBACK_URL))
            {
              //set visibility  GONE/INVISIBLE for webview.
            }
            else if(url.startsWith("http")) 
            {
                view.loadUrl(url);
                return true;
            }

            return false;
        }
Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
  • hi, I returns true after setting webView GONE but... it works weird a bit. It returns to the current activity and the webView is gone. I mean the onNewIntent isn't called!!! near to work. – emeraldhieu Aug 02 '12 at 07:06
  • onNewIntent will call when another Activity start your activity with flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. So when you was using browser then browser start your activity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag. But in case of webview onNewIntent will never call. – Vivek Kumar Srivastava Aug 02 '12 at 08:10
  • so in if condition for call_back url you can write code to get access_token or whatever you need after twitter login. – Vivek Kumar Srivastava Aug 02 '12 at 08:12
0

I think you should use that like that: 1),

public static final String CALLBACK_URL = "test123://TwitterLoginActivity";

2),

<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"  android:host="TwitterLoginActivity"/>
        </intent-filter>
    </activity>

Pls test it.Good luck!

enjoy-writing
  • 520
  • 3
  • 4