putExtra()不起作用

时间:2015-07-04 19:03:30

标签: java android android-intent android-activity

我的问题是在3个活动之间传递一个字符串。我解释了我是如何做到的。

我执行此操作的第一个名为loginActiviy的活动。

  //OnCreate ecc before this
     @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     data.putExtra("Name", Name);
     super.onActivityResult(requestCode, resultCode, data);
 }

 public void onResume()
 {
     super.onResume();
     finish();
 }

当活动结束时称为finish()(也许是我不知道的问题)然后控制转到第一个活动,我这样做

    //OnCreate ecc before
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle extras = data.getExtras();
    String name = null;
    if(extras != null) {
        name = extras.getString("Name");
    }
    super.onActivityResult(requestCode, resultCode, data);
    try
    {
        Intent people = new Intent(this, MainPeopleActivity2.class);
        people.putExtra("Name", name);
        startActivity(people);
        this.finish();
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), e, Toast.LENGTH_LONG).show();
    }
}

现在开始第三项活动,其中意图执行此操作

        String name;
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            name= null;
        } else {
            name= extras.getString("Name");
        }
    } else {
        name = (String) savedInstanceState.getSerializable("Name");
    }

但是你可以想象不是这种情况,字符串是空的。

我错在哪里?提前谢谢。

这是完整的登录类

                public void onSuccess(LoginResult loginResult) {
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject user, GraphResponse response) {
                                if (response.getError() != null) {
                                } else {
                                    id = user.optString("id");
                                    firstName = user.optString("first_name");
                                    lastName = user.optString("last_name");
                                    email = user.optString("email");
                                    Log.i(TAG,"User ID "+ id);
                                    Log.i(TAG,"Email "+ email);
                                }
                                Name = firstName + " " + lastName;
                                Toast.makeText(getApplicationContext(), "Log in with " + Name, Toast.LENGTH_LONG).show();
                            }
                        }).executeAsync();
        }

2 个答案:

答案 0 :(得分:1)

以下是您可以解决问题并使代码正常工作的方式。

您需要onActivityResult()的唯一地方是活动A(第一个活动)。

此过程的第一步是使用startActivityForResult()启动您的LoginActivity,可能来自onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this, LoginActivity.class);
    startActivityForResult(i, 999);

}

然后,在此类中设置onActivityResult()方法,该方法将处理LoginActivity的结果,并将Name发送给MainPeopleActivity2:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //check request code and result code:
    if (requestCode == 999 && resultCode == RESULT_OK) {
        //get the extras:
        Bundle extras = data.getExtras();
        String name = null;
        if (extras != null) {
            name = extras.getString("Name");
            try {
                Intent people = new Intent(this, MainPeopleActivity2.class);
                people.putExtra("Name", name);
                startActivity(people);
                //this.finish();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

然后,您在LoginActivity中的onSuccess()方法将需要一些添加的代码。通过调用setResult()

将名称发回第一个活动
public void onSuccess(LoginResult loginResult) {
    GraphRequest.newMeRequest(
            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse response) {
                    if (response.getError() != null) {
                        //process error
                    } else {
                        id = user.optString("id");
                        firstName = user.optString("first_name");
                        lastName = user.optString("last_name");
                        email = user.optString("email");
                        Log.i(TAG,"User ID "+ id);
                        Log.i(TAG, "Email " + email);

                        Name = firstName + " " + lastName;
                        Toast.makeText(getApplicationContext(), "Log in with " + Name, Toast.LENGTH_LONG).show();

                        //adding this:
                        Intent i = new Intent();
                        i.putExtra("Name", Name);
                        LoginActivity.this.setResult(RESULT_OK, i);
                        LoginActivity.this.finish();
                    }


                }
            }).executeAsync();
}

然后,您将能够在onCreate()中的MainPeopleActivity2活动中成功获取名称:

public class MainPeopleActivity2 extends ActionBarActivity {

    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_MainPeopleActivity2);

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                name= null;
            } else {
                name= extras.getString("Name");
            }
        } else {
            name = (String) savedInstanceState.getSerializable("Name");
        }

    }

  //...... rest of the code in this class

答案 1 :(得分:0)

将结果从被调用的Activity传递回调用Activity的正确方法是在被调用的Activity中使用Activity.setResult(int,Intent):http://developer.android.com/reference/android/app/Activity.html#setResult(int,android.content.Intent)

调用Activity必须调用startActivityForResult()而不是startActivity()才能获得结果。