0

I'm using Twitter4J to authorise with Twitter from an activity which is not the application's launch activity. So far I have a separate activity called 'AuthorisationActivity' which returns an intent with a valid Twitter4j twitter object which then can be used by the calling activity 'CallAuthorisationActivity'.
The callback from twitter works fine and I end up with an authorised twitter object in AuthorisationActivity, but now I find myself in a catch22 situation: 1 - If I DON'T set the AuthorisationActivity's Launch mode to 'singleTask', the callback will not return to AuthorisationActivity's onNewIntent() method, but to its onCreate() method and try to authenticate in an endless loop. 2 - If I DO set the AuthorisationActivity's Launch mode to 'singleTask', it will return to onNewIntent() but since it now is in a separate task, it will not return to the calling activity's onActivityResult() method.

I'm only a beginner in Android development, and since the authentication works, I assume that this problem has more to do with how the activites are managed. Below is a simplified excerpt from the manifest:

<activity 
    android:name=".MainActivity">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter>
</activity>

<activity
    android:name=".CallAuthorisationActivity">
</activity>

<activity 
    android:name=".AuthorisationActivity"
    android:label="@string/title_activity_authorisation">  
    <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="tweetree" android:host="callback" />
    </intent-filter>
</activity> 

In the code, CallAuthorisationActivity just sends an intent with startActivityForResult(...) to AuthorisationActivity and catches the result in onActivityResult(...) as is standard. Any help would be appreciated, thanks

1 Answers1

0

After some hours of research I came upon this link http://gtware.blogspot.com.au/2012/07/tweeting-from-android-apps-using.html#comment-form which after some changes provided me with an elegant solution.

As I commented on that page: Using a WebView makes it much easier to handle callbacks since the browser is integrated in the application. So there's no need for intent filters at all.

I just changed a few things you may be interested in:

1 - I'm using a Twitter object instead of a AsyncTwitter. The Twitter object seems to make it easier to get GET data from twitter, such as from the User search API, and it doesn't need a TwitterListener.

2 - My virtual Device crashes when calling twitter.getOAuthRequestToken() and twitter.getOAuthAccessToken(), even with AsyncTwitter objects, so I encapsulated these in their own AsyncTask threads.

Hope this may help...