Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:24:37+00:00 2026-06-08T13:24:37+00:00

I am using Twitter4j API in android, when I press back button from the

  • 0

I am using Twitter4j API in android, when I press back button from the twitter browser screen to cancel authorization, it takes me back to onResume() of PrepareRequestTokenActivity (activity provided by twitter4j api) and I am finishing this activity from onResume, but when authorization is successful, then also it is going to onResume() and then to the onNewIntent() method of same activity, but activity get finished from on resume and authentication get failed. Is there any way to capture back button from twitter browser screen? below is the activity.

public class PrepareRequestTokenActivity extends Activity {



        public static final String CONSUMER_KEY = "Hr8aDOFeDdY9UbvQB0w2w";
        public static final String CONSUMER_SECRET= "wfZOJYkYVEYrmdmltOaKfRdnUfSiUkr2MQdjRUY2xU";

        public static final String REQUEST_URL = "http://twitter.com/oauth/request_token"; //"https://api.twitter.com/oauth/request_token"
        public static final String ACCESS_URL = "http://twitter.com/oauth/authorize"; //"https://api.twitter.com/oauth/authorize"
        public static final String AUTHORIZE_URL = "http://twitter.com/oauth/access_token"; //"https://api.twitter.com/oauth/access_token"

        final public static String OAUTH_CALLBACK_SCHEME = "droidnotify-oauth-twitter";
        final public static String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://callback";

        private boolean _debug = false;
    private OAuthConsumer _consumer; 
    private OAuthProvider _provider;


        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                _debug = Log.getDebug();
                if (_debug) Log.v("PrepareRequestTokenActivity.onCreate()");
        try {
                _consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            //_provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
            _provider = new DefaultOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
        } catch (Exception ex) {
                if (_debug) Log.e("PrepareRequestTokenActivity.onCreate() Error creating consumer / provider: " + ex.toString());
                }
        if (_debug) Log.v("PrepareRequestTokenActivity.onCreate() Starting task to retrieve request token.");
                new OAuthRequestTokenTask(this, _consumer, _provider).execute();
        }


        @Override
        public void onNewIntent(Intent intent) {
                super.onNewIntent(intent); 
                if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent()");
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                final Uri uri = intent.getData();
                if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
                        if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent() Callback received : " + uri);
                        if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent() Retrieving Access Token");
                        new RetrieveAccessTokenTask(this, _consumer, _provider, prefs).execute(uri);
                        finish();       
                }
        }


        public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {

                private Context _context;
                private OAuthProvider _provider;
                private OAuthConsumer _consumer;
                private SharedPreferences _prefs;


                public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) {
                        _context = context;
                        _consumer = consumer;
                        _provider = provider;
                        _prefs=prefs;
                }


                @Override
                protected Void doInBackground(Uri...params) {
                        final Uri uri = params[0];
                        final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
                        try {
                                _provider.retrieveAccessToken(_consumer, oauth_verifier);
                                final Editor edit = _prefs.edit();
                                edit.putString(OAuth.OAUTH_TOKEN, _consumer.getToken());
                                edit.putString(OAuth.OAUTH_TOKEN_SECRET, _consumer.getTokenSecret());
                                edit.commit();
                                String token = _prefs.getString(OAuth.OAUTH_TOKEN, "");
                                String secret = _prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
                                _consumer.setTokenWithSecret(token, secret);
                                //_context.startActivity(new Intent(_context, AndroidTwitterSample.class));
                                //executeAfterAccessTokenRetrieval();
                                Toast.makeText(_context, "Twitter Authentication Successfull", Toast.LENGTH_LONG);
                                Toast.makeText(_context, "OAuth.OAUTH_TOKEN KEY: " + OAuth.OAUTH_TOKEN + ", OAuth.OAUTH_TOKEN Value: " + _consumer.getToken(), Toast.LENGTH_LONG);
                                Toast.makeText(_context, "OAuth.OAUTH_TOKEN_SECRET KEY: " + OAuth.OAUTH_TOKEN_SECRET + ", OAuth.OAUTH_TOKEN_SECRET Value: " + _consumer.getTokenSecret(), Toast.LENGTH_LONG);
                                if (_debug) Log.v("OAuth - Access Token Retrieved");

                        } catch (Exception ex) {
                                if (_debug) Log.e("OAuth - Access Token Retrieval Error: " + ex.toString());
                        }
                        return null;
                }


        }       

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:24:39+00:00Added an answer on June 8, 2026 at 1:24 pm

    Just take one boolean flag initialize with false make it true in onNewIntent() function of your PrepareRequestTokenActivity and add one if condition in your onResume function that if this flag is false then finish activity and if true dont do anything , i.e when you click back button from browser it directly goes to onResume at that point your flag will be false then this activity will finished and when on success or No thanks it will first go to onNewIntent() then onResume , so true then activity will not finished

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a java Twitter app (using Twitter4J api). I have created
I am using twitter4j library to access twitter api. I can sucessfully retrieve information
I am using twitter4j java library to make a twitter API search for given
Using the Twitter API, and just want to have a simple 'reply' button on
I'm using the Twitter API in order to pull tweets from specific users. I
I am trying to integrate twitter api using library twitter4j-core-2.1.1-SNAPSHOT.jar. But i am getting
Possible Duplicate: Can we post image on twitter using twitter API in Android? I
I've created an android app using twitter4j. None of the api calls can authenticate
So I'm parsing data from twitter api in rails using the twitter library, and
I am making an app for twitter integration using Twitter4j API. Right now my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.