6

My app has 3 activity A, B, C. Activity A calls B. In B, I call Intent.ACTION_VIEW to do authentication with Twitter as below:

public static void DoAuthen(Context context, String CallBackUrl) throws OAuthMessageSignerException, OAuthNotAuthorizedException,
        OAuthExpectationFailedException, OAuthCommunicationException {
    httpOauthConsumer = new CommonsHttpOAuthConsumer(context.getString(R.string.Twitter_ConsumerKey), context
            .getString(R.string.Twitter_ConsumerSecret));
    httpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token",
            "http://twitter.com/oauth/authorize");
    String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CallBackUrl);
    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}

After authentication, My App is called back at activity B. Here B calls C. Now if I press Back button, it will navigate to browser (which used to authenticate with Twitter before) rather than to B and then to A. How can I resolve this?

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165

3 Answers3

9

Please refer to Tasks and Back stack in android. You can use two tasks in your application - in first one you do your business, in second - authorization. You start authorization with intent flag FLAG_ACTIVITY_NEW_TASK and use parameter android:clearTaskOnLaunch. Good luck!

Gregory Kalabin
  • 1,760
  • 1
  • 19
  • 45
  • Thanks Grigory, Could you please give me some sample codes? I can set flag FLAG_ACTIVITY_NEW_TASK when I call Intent.ACTION_VIEW but I dont know how to use parameter android:clearTaskOnLaunch with this. – Nguyen Minh Binh Jul 17 '11 at 15:06
  • 1
    @nguyen-minh-binh try to read this: http://intridea.com/2011/6/16/android-understanding-activity-launchmode?blog=company – Gregory Kalabin Jul 17 '11 at 15:13
1

I added the following flags to the ACTION_VIEW intent and it solved the going back to browser problem

consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
                    "http://twitter.com/oauth/access_token",
                    "http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL);
Intent oauthIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));        
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
oauthIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
kitwalker
  • 862
  • 1
  • 10
  • 29
0

In C you can override back button to go directly to B

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        startActivity(C.this,B.class);
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Rasel
  • 15,499
  • 6
  • 40
  • 50
  • I have tried this way before. It works. But when I press Home button to minimize my app then press and hold button Home to open my app again, I will show activity B instead of A. – Nguyen Minh Binh Jul 17 '11 at 14:59