再次恢复第一个活动而不是第二个活动

时间:2013-08-30 09:47:23

标签: android twitter4j

我在twitter android链接app中使用了两个活动。第一个活动是LoginActivity,第二个是TweetslistActivity。我在loginActivity中使用了一个按钮,点击后,Twitter登录对话框打开。当用户成功登录时,我的应用程序应该打开第二个活动。但在用户通过身份验证后,我的第一个活动将恢复,我必须再次单击“登录”按钮才能进入第二个活动!

 the logcat shows:
08-30 05:05:48.477: I/dalvikvm-heap(4205): Grow heap (frag case) to 6.686MB for 4194320-byte allocation
08-30 05:05:48.557: D/dalvikvm(4205): GC_FOR_ALLOC freed 0K, 3% free 6732K/6920K, paused 76ms, total 76ms
08-30 05:05:49.288: D/gralloc_goldfish(4205): Emulator without GPU emulation detected.
08-30 05:05:55.757: V/tweetsData--(4205): null
08-30 05:06:11.897: E/Twitter Login Error(4205): > No Token available.

我还在Manifest文件中使用了'android:launchMode =“singleTask”'。但它只是指导我从第一次活动到第二次没有登录!我只是卡住了但没有得到任何答案..请提出一些好的解决方案。提前谢谢你。

修改

原始代码:

public class TwitterLoginActivity extends Activity {
    TextView tv;
    Button loginBtn;
    ImageView logo;
    private ConnectionDetector cd;
    static Twitter twitter;
    ProgressDialog pDialog;
    ArrayList<String> tweets;
    String tweetsData;

    private static SharedPreferences mSharedPreferences;
    AlertDialogManager alert = new AlertDialogManager();
    RequestToken requestToken;
    AccessToken accessToken;
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.twitter_login);
            tweets = new ArrayList<String>();
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            if (android.os.Build.VERSION.SDK_INT > 9) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll().build();
                    StrictMode.setThreadPolicy(policy);
            }
            cd = new ConnectionDetector(getApplicationContext());
            if (!cd.isConnectingToInternet()) {
                    alert.showAlertDialog(TwitterLoginActivity.this,
                                    "Internet Connection Error",
                                    "Please connect to working Internet connection", false);
                    return;
            }

            if (CONSUMER_KEY.trim().length() == 0
                            || CONSUMER_SECRET.trim().length() == 0) {
                    // Internet Connection is not present
                    alert.showAlertDialog(TwitterLoginActivity.this, "Twitter oAuth tokens",
                                    "Please set your twitter oauth tokens first!", false);
                    return;
            }

            tv = (TextView) findViewById(R.id.welcome_txt);
            loginBtn = (Button) findViewById(R.id.login_btn);
            logo = (ImageView) findViewById(R.id.imageView1);
            mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref",
                            0);

            loginBtn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            Thread th = new Thread(new Runnable() {
                                    public void run() {
                                            loginToTwitter();
                                    }
                            });
                            th.start();

                    }
            });

            if (!isTwitterLoggedInAlready()) {
                    Uri uri = getIntent().getData();
                    if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
                            uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

                            try {

                                    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken);
                                    Editor e = mSharedPreferences.edit();
                                    e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                                    e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret());
                                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                                    e.commit();

                                    Log.e("Twitter OAuth Token", "> " + accessToken.getToken());

                            }
                            catch (Exception e) {
                                    Log.e("Twitter Login Error", "> " + e.getMessage());
                            }
                    }
            }

    }

    public void loginToTwitter() {
            if (!isTwitterLoggedInAlready()) {
                    ConfigurationBuilder builder = new ConfigurationBuilder();
                    builder.setOAuthConsumerKey(CONSUMER_KEY);
                    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
                    Configuration configuration = builder.build();
                    TwitterFactory factory = new TwitterFactory(configuration);
                    twitter = factory.getInstance();
                    try {
                            requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
                            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                            .parse(requestToken.getAuthenticationURL()))); }
                            });

                    }
                    catch (TwitterException e) {
                            e.printStackTrace();
                    }
            }
                    Intent intent = new Intent(TwitterLoginActivity.this, TweetsListActivity.class);
                    intent.putExtra("tweetsdata",tweetsData);
                    startActivity(intent);

                    finish();               }


    private boolean isTwitterLoggedInAlready() {
            return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
    }

    @Override
    protected void onResume() {
            super.onResume();

    }

}

1 个答案:

答案 0 :(得分:0)

使用我的更改将onCreate()末尾的代码放到onResume()上:

Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
    uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

    try {
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken);
        Editor e = mSharedPreferences.edit();
        e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
        e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret());
        e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
        e.commit();

        Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
    }
    catch (Exception e) {
        Log.e("Twitter Login Error", "> " + e.getMessage());
    }
}

if (!isTwitterLoggedInAlready()) {
    // Goto TweetsListActivity
    Intent intent = new Intent(TwitterLoginActivity.this, TweetsListActivity.class);
    intent.putExtra("tweetsdata",tweetsData);
    startActivity(intent);
    finish();
}

另请注意,在第127-128行的代码中,这些行被切换:

     startActivity(intent);
     intent.putExtra("tweetsdata",tweetsData);

在开始活动之后,无意改变意图。

修改

在web登录的startActivity()之后也注意到这段代码,这没有任何意义:

TwitterLoginActivity.this.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(TwitterLoginActivity.this,
                TweetsListActivity.class);
        intent.putExtra("tweetsdata",tweetsData);
        startActivity(intent);
        finish();
    }
});

删除此代码。