Android: Twitter login using twitter4j library

... I set up Twitter user authentication as in the tutorial above such that the Activity doing the Twitter authentication has its content view changed to a WebView, the Activity is declared in the manifest file containing an intent-filter to catch the Twitter callback and, on callback, the response is processed and the Activity's content view is changed back to whatever it was.

I got it working but in doing so I found (what I think is) an easier way and such that the WebView doesn't ever kick off the phone's web browser app (thereby leaving your app, which you don't really want), as follows:

(1) Remove the ActivitysingleInstance and intent-filter declarations you added to the manifest file (in following the tutorial above). Don't need that anymore!

(2) Add a WebView to your Activity layout file and set its initial visibility to "gone" and its position, height, width etc however you want it. I set its properties so that it covers the whole screen when visible, as follows:

Most likely your app is crashing because you're calling the 'Twitter.getOAuthRequestToken(...)' method in the main ui thread of your activity and not in a background/async task. Call it instead in an 'AsyncTask.doInBackground(...)' method and it should work fine.

And your callback url can be anything really as long as it's the same in all places within your app.

Thank you for the fast answer. I am trying to run twitter since 3 weeks for my application and nothing works.

Is it possible to explain this more detail?Do I have to implement a new Class which extends from AsyncTask<> and write the doInBackgroundMethod() like this:protected RequestToken doInBackground(Twitter...params){ return twitter.getOAuthRequestToken( "myapptwittercallback:///myapp" );}

and then instead of the call I do: request = AsyncClass.doInBackground()?

mHandler.post(new Runnable(){ @Override public void run() { // We're authenticated - we can now tweet! // This must run on the main UI thread... e.g. you can now post, if you want! userLoggedIn(); } });