0

First time posting a question and I apologize, there are a ton of questions on this issue but after trying many different options I can't seem to get this to work.

I have a twitter application that the main screen has a button that requests oauth twitter access through the standard browser app, and then returns to the main screen. If I hit the back button, the browser app opens with the twitter website. I've tried noHistory in the manifest file, and when starting the intent FLAG_ACTIVITY_NO_HISTORY as well as FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP in many scenarios with no luck.

Here is my latest code:

Manifest file

<activity android:name=".oauth" 
                  android:label="@string/oauth_name"
                  android:noHistory="true"
                  >
            <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="tm" android:host="twitt" />

            </intent-filter>
        </activity> 
<activity   android:name="com.tweetymanagerpro.Home" 
                    android:label="@string/app_name"
                    android:theme="@android:style/Theme.NoTitleBar"
                    android:windowSoftInputMode="stateHidden|adjustPan">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                </intent-filter>
          </activity>

In the Home activity, I call the oauth as follows

Intent settingsActivity = new Intent(getBaseContext(), oauth.class);
                settingsActivity.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(settingsActivity);

Next, in oauth.onCreate() I call for the twitter url

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

            this.startActivity(intent);

Next, in oauth.onResume() I handle the tokens etc and then start Home again

Intent i = new Intent(this, Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

The connection to twitter works great, it's just now at Home if I hit the back button it takes me back to the browser where it is authenticating with twitter.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Rob
  • 43
  • 5

2 Answers2

1

Consider rendering the oauth web page in a webview you control rather than launching out to the browser app.

The oauth web page, when it calls its callback, will actually be replacing itself in the webview, where your WebViewClient can catch that expected URL, process the results, and finish() the webview (removing it from the activity stack).

larham1
  • 11,736
  • 5
  • 35
  • 26
  • I +1'd this, even though I haven't tried it. I think it is a good idea and will probably go this way – Boy Sep 01 '14 at 15:06
  • I know this is an old thread, but to warn people who might consider using a WebView to handle the OAuth flow. Google currently [recommends against using a WebView and instead promotes the use of the device's browser for OAuth](https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html) – ricalo Sep 13 '16 at 16:34
0

Is the oauth Activity the actual browser Activity? If so you can call finish() on it after you start the Home Activity in it to remove it from the activity stack.

If not, maybe if your Home Activity is started from your oauth Activity, your oauth class adds another flag in your intent. So if your home class sees this intent, it can override the BACK key press and not perform the default action (finish and resume to your browser Activity).

triad
  • 20,407
  • 13
  • 45
  • 50
  • Thanks for the response. The actual call to the browser activity is the code above with Uri.parse(authUrl). I have tried using finish, I'll add it back in and post the results. – Rob Feb 28 '12 at 02:36
  • I added the finish() call after each call to a new activity and now once I'm returned to the home activity, I hit the back button and there is a very quick flash of the select app to launch activity screen, and that's it. A second hit of the back button closes the app. That'll work for me, thanks for the info triad! – Rob Feb 28 '12 at 05:56