是的,我也遇到过这个问题。
许多开发人员在通过 Intent 或 Bundle 将数据从对话框传递到另一个活动时也面临问题。它在从另一个活动中检索时返回 null。
唯一的解决方案是 SharedPreferences。
但是您必须将其放在关闭按钮内。(例如:确定/取消等)
并通过相同的密钥轻松地从另一个活动中检索数据。不要使用任何带有广播意图的服务。
对话活动中的代码是这样的:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.mailicon);
builder.setTitle(name);
builder.setView(view);
builder.setPositiveButton("Send Request",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
String mailID = id.getText().toString();
//Here define all your sharedpreferences code with key and value
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("MID", mailID );
edit.commit();
}
});
并从另一个获取这样的数据:
SharedPreferences bb = getSharedPreferences("my_prefs", 0);
String m = bb.getString("NUM", "");
Toast.makeText(this, m, Toast.LENGTH_SHORT).show();
添加一些检查以获得良好的标准。
谢谢