2011-07-17 26 views
4

我的應用程序有3個活動A,B,C活性A調用B.在B,我叫Intent.ACTION_VIEW來與Twitter做認證如下:去呼叫Intent.ACTION_VIEW回來後的Android

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))); 
} 

認證後,我的應用程序在活動B處被回調。這裏B調用C. 現在,如果我按下「後退」按鈕,它將導航到瀏覽器(之前用於使用Twitter進行認證),而不是B再到A. 我該如何解決這個問題?

+0

如果B調用C按回到℃之後,它也應該回到B ..怪知道..我也會嘗試重現這個 – Javanator

+0

是的,沒錯。但是,如果B在調用C之前調用Intent.ACTION_VIEW(請求Twitter頁面),它不會直接從C返回給B B –

+0

您在評論中寫的是您的認證活動返回到b,然後b調用c ..這意味着認證活動必須完成.. – Javanator

回答

8

請參考Tasks and Back stack in android。您可以在應用程序中使用兩項任務 - 第一項是您的業務,第二項是授權。您使用意向標誌FLAG_ACTIVITY_NEW_TASK開始授權並使用參數android:clearTaskOnLaunch。祝你好運!

+0

感謝Grigory,請給我一些示例代碼?我可以設置標誌FLAG_ACTIVITY_NEW_TASK當我調用Intent.ACTION_VIEW,但我不知道如何使用參數android:clearTaskOnLaunch與此。 –

+1

@ nguyen-minh-binh嘗試閱讀以下內容:http://intridea.com/2011/6/16/android-understanding-activity-launchmode?blog=company –

0

在C語言中,你可以重寫回按鈕,直接進入到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); 
} 
+0

我已經嘗試過這種方式。有用。但是,當我按Home按鈕來最小化我的應用程序,然後按住Home按鈕再次打開我的應用程序,我將顯示活動B而不是A. –

1

我添加以下標誌的ACTION_VIEW意圖,它解決了回去瀏覽器問題

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);