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

  • Home
  • SEARCH
  • 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 6045597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:08:33+00:00 2026-05-23T07:08:33+00:00

I am using Twitter4J to post to Twitter but you have to open up

  • 0

I am using Twitter4J to post to Twitter but you have to open up the browser and get a pin from Twitter for my app to be able to do this and that works but when I return to my app, the activity times out and closes itself. Is there a way to keep my activity open? The webview does not work as Twitter doesn’t let you authorize with a regular path.

public void TwitterSend () {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumer_token, consumer_secret);

    //WebView webview = (WebView) findViewById(R.id.webview);
    //webview.setVisibility(View.VISIBLE);
    //ScrollView sc = (ScrollView) findViewById(R.id.scrollView1);
    //sc.setVisibility(View.VISIBLE);
    EditText edit = (EditText) findViewById(R.id.editText1);
    edit.setVisibility(View.VISIBLE);
    RequestToken requestToken = null;
    try {
        requestToken = twitter.getOAuthRequestToken();
        System.out.println(requestToken.toString());
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    AccessToken accessToken = null;

    //webview.loadUrl("https://api.twitter.com/oauth/authorize");
    Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse(requestToken.getAuthorizationURL()));
    browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(browserIntent);


      System.out.println("Open the following URL and grant access to your account:");

      System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
      String pin = edit.getText().toString();
    CountDownTimer timer = new CountDownTimer(900000, 1000) {

        public void onTick(long millisUntilFinished) {
        }


        public void onFinish() {

        }
     };
     timer.start();
      while(pin.length()<7)
      {
          pin = edit.getText().toString();
      }
      System.out.print(pin);
      try{
         if(pin.length() > 0){
           accessToken = twitter.getOAuthAccessToken(requestToken, pin);
         }else{
           accessToken = twitter.getOAuthAccessToken();
         }
      } catch (TwitterException te) {
        if(401 == te.getStatusCode()){
          System.out.println("Unable to get the access token.");
            edit.setVisibility(View.GONE);
        }else{
          te.printStackTrace();
        }
      }

    //persist to the accessToken for future reference.
    Status status = null;
    try {

        SharedPreferences stats = getSharedPreferences(PREFS_NAME, 0);
        String quote = stats.getString("shareQuote", "An error has occured. We are Sorry.");
        status = twitter.updateStatus(quote);
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        //webview.setVisibility(View.GONE);
        //edit.setVisibility(View.GONE);
    }
     Toast.makeText(getApplicationContext(), "Successfully updated the status to [" + status.getText() + "].", Toast.LENGTH_LONG).show();
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
   // webview.setVisibility(View.GONE);
    edit.setVisibility(View.GONE);
    //sc.setVisibility(View.GONE);
    /*WebView webview = (WebView) findViewById(R.id.webview);
    webview.setVisibility(View.VISIBLE);
    Twitter twitter=new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumer_token, consumer_secret);
    AccessToken a = new AccessToken(oauth_token, oauth_token_secret);
    twitter.setOAuthAccessToken(a);
    try {
        RequestToken requestToken = twitter.getOAuthRequestToken("https://api.twitter.com/oauth/request_token");
        webview.loadUrl("https://api.twitter.com/oauth/authorize");
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/

}
  • 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-05-23T07:08:34+00:00Added an answer on May 23, 2026 at 7:08 am

    It’s generally a bad idea in Android to try to force activities to stay open – the best way to deal with activities being destroyed is to make sure that you are handling all of your activities’ lifecycle callbacks properly. This page is helpful: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    If you properly save the state of your activity, you shouldn’t have problems with the system stopping it.

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

Sidebar

Related Questions

I have integrated twitter in my app. but I am not able to log
I am working on an iOS app that needs to post to either Twitter,
I had successfully integrate twitter API and I am able to post text from
I have successfully created an app to login user and post the tweet using
Right now I'm using this which works for the development host, but I have
Ok twitter might be great for social media but for a small app that
When using simple request to post to Twitter the messages are signed with sent
I'm calling the twitter4j library using Clojure like so: (def twitter (. (TwitterFactory.) getInstance))
I'm trying to create a topbar using twitter bootstrap, but the black bar won't
How can I post a message to twitter using only javascript (no serverside authentification).

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.