gpt4 book ai didi

android - 无法从两个不同的 Activity 中获取 bundle 数据

转载 作者:行者123 更新时间:2023-11-29 17:45:49 25 4
gpt4 key购买 nike

我能够从每个 Activity 中获取数据,但在同时从两个 Activity 中调用时无法获取数据。

我有3个 Activity

1.Google登录2.Facebook登录3.主要 Activity

我的 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;
}

我的 Facebook 登录名中有这个:

          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,但在能够从每个 Activity 中调用时能够单独获取它们。

最佳答案

根据逻辑流程,您的 Activity 可以通过 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);
}

}

关于android - 无法从两个不同的 Activity 中获取 bundle 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com