[android]putExtra() 不工作

发布时间: 2017/3/19 14:24:59
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我的问题是要传递一个字符串之间 3 activity 。怎么它说明

第一个 activity 称为 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();
 }

activity 两端称为完成 () (也许这就是我不知道的问题) 然后控制将传递到第一个 activity ,哪里这

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

现在开始第三 activity ,在 intent 这样做

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

解决方法 1:

这里是如何它看起来像你可以解决你的问题,而你代码工作。

你需要的唯一的地方 onActivityResult() 是在 Activity A (第一 Activity )。

第一步 process 即将推出你 LoginActivity 使用 startActivityForResult() ,大概从 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() 方法在此类,将 process LoginActivity 和发送名称到 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();
            }
        }
    }
}

然后你 onSuccess() LoginActivity 方法将需要一些添加的代码在这里。将名称发送回第一 Activity 调用 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();
}

然后,你将能够成功获得的名称是在你的 MainPeopleActivity2 ActivityonCreate() :

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
官方微信
官方QQ群
31647020