无法从两个不同的活动中获取捆绑数据

时间:2014-11-09 06:36:24

标签: android android-intent android-activity bundle

我能够从每个单独的活动中获取数据,但是一次从两个活动中调用时都无法获取数据。

我有3项活动

1.GoogleLogin 2.FacebookLogin 3.MainActivity

我在GoogleLogin中有这个:

  @Override
  public void onConnected(Bundle connectionHint) {
    // Reaching onConnected means we consider the user signed in.

    Intent i = new Intent(getApplicationContext(),MainActivity.class);
    Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  //Create the bundle
    Bundle bundle = new Bundle();
    //Add your data from getFactualResults method to bundle
    bundle.putString("Google", "Logged in using Google Account");
    bundle.putString("GoogleUsername",  currentUser.getDisplayName());
    //Add the bundle to the intent
    i.putExtras(bundle);
    startActivity(i);


    // Indicate that the sign in process is complete.
    mSignInProgress = STATE_DEFAULT;
  }

我在FacebookLogin中有这个:

          if (session.isOpened()) {

          // make request to the /me API
          Request.newMeRequest(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {

              Intent i = new Intent(FBMainActivity.this,MainActivity.class);
                  //Create the bundle
                    Bundle bundle = new Bundle();
                    //Add your data from getFactualResults method to bundle
                    bundle.putString("Facebook", "Logged in using Facebook Account");
                    bundle.putString("LastName", user.getLastName());
                    bundle.putString("FirstName", user.getFirstName());
                    //Add the bundle to the intent
                    i.putExtras(bundle);
                    startActivity(i);
              }
            }
          }).executeAsync();
        }

在我的MainActivity中,我得到它们如下所示:

       // 1. get passed intent 
       Intent facebookintent = getIntent();

       // 2. get message value from intent
       String lastName = facebookintent.getStringExtra("LastName");
       String firstName = facebookintent.getStringExtra("FirstName");

       // 3. get bundle from intent
       Bundle facebookbundle = facebookintent.getExtras();

       // 4. get status value from bundle
       String facebookstatus = facebookbundle.getString("Facebook");



       // 1. get passed intent 
       Intent googleintent = getIntent();

       // 2. get message value from intent
       String userName = googleintent.getStringExtra("GoogleUsername");

       // 3. get bundle from intent
       Bundle googlebundle = facebookintent.getExtras();

       // 4. get status value from bundle
       String googlestatus = googlebundle.getString("Google");

       if (facebookstatus=="Logged in using Facebook Account"){ 

           // 3. show message on textView 
           ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName);

       }else if (googlestatus=="Logged in using Google Account"){ 

           // 3. show message on textView 
           ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName);
       }

我无法获得GoogleUsername,但能够从每个活动中调用时能够单独获取它们。

2 个答案:

答案 0 :(得分:2)

根据逻辑流程,您的活动可以通过facebookgoogle登录来开始。 所以你必须适当地检查和使用它。做这样的事

  // 1. get passed intent 
   Intent intent = getIntent();

   if (intent.getStringExtra("Facebook") != null){

     // 2. get message value from intent
     String lastName = intent.getStringExtra("LastName");
     String firstName = intent.getStringExtra("FirstName");

     if(intent.getStringExtra("Facebook").equals("Logged in using Facebook Account")){
       ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + lastName + " " + firstName);
     }
 }else if(intent.getStringExtra("Google") != null){ 


     // 2. get message value from intent
     String userName = googleintent.getStringExtra("GoogleUsername");

     // 3. get bundle from intent
     Bundle googlebundle = facebookintent.getExtras();

     if(intent.getStringExtra("Google").equals("Logged in using Google Account")){
      ((TextView)findViewById(R.id.txtUser)).setText("Hello" + " " + userName);
     }

 }

答案 1 :(得分:1)

在检查googleStatus和facebookStatus之前需要检查。 (在java中我们不将两个String与==进行比较,将其与.equals()方法进行比较)

您需要检查您的捆绑包中是否存在Google帐户或Facebook帐户,因为您需要以下代码:

 if (bundle != null)
 {

     if ( bundle.containsKey("Facebook") )
     {    
       // user logged in with facebook account
     } 
     else if (bundle.containsKey("Google") )
     {
       // check google account
     }

}