Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go back after call Intent.ACTION_VIEW in android

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?

like image 764
Nguyen Minh Binh Avatar asked Jul 17 '11 14:07

Nguyen Minh Binh


People also ask

How can I go back programmatically in Android?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

What is Intent setType in Android?

setType(String mimeType) input param is represent the MIME type data that u want to get in return from firing intent(here myIntent instance). by using one of following MIME type you can force user to pick option which you desire. Please take a Note here, All MIME types in android are in lowercase.

What is Action_view?

ACTION_VIEW is the action on Intent. public static final String ACTION_VIEW. Since: API Level 1 Activity Action: Display the data to the user. This is the most common action performed on data -- it is the generic action you can use on a piece of data to get the most reasonable thing to occur.


2 Answers

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!

like image 191
Gregory Kalabin Avatar answered Oct 26 '22 22:10

Gregory Kalabin


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);
like image 38
kitwalker Avatar answered Oct 27 '22 00:10

kitwalker