我有两个活动:主活动和儿童活动。 当我按下主活动中的按钮时,子活动就启动了。

现在我想把一些数据发送回主屏幕。我使用了Bundle类,但它不起作用。它会抛出一些运行时异常。

有什么解决办法吗?


当前回答

使用sharedPreferences并保存数据并从应用程序中的任何地方访问它

像这样保存日期

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();

然后像这样接收数据

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String savedPref = sharedPreferences.getString(key, "");
    mOutputView.setText(savedPref);

其他回答

使用sharedPreferences并保存数据并从应用程序中的任何地方访问它

像这样保存日期

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();

然后像这样接收数据

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String savedPref = sharedPreferences.getString(key, "");
    mOutputView.setText(savedPref);

只是一个小细节,我认为是错过了以上的答案。

如果你的子活动可以从多个父活动中打开,那么你可以根据你的活动是由startActivity还是startActivityForResult打开来检查你是否需要做setResult。您可以通过使用getCallingActivity()来实现这一点。更多信息请点击这里。

Activity 1使用startActivityForResult:

startActivityForResult(ActivityTwo, ActivityTwoRequestCode);

启动Activity 2,你可以执行以下操作,关闭Activity:

Intent output = new Intent();
output.putExtra(ActivityOne.Number1Code, num1);
output.putExtra(ActivityOne.Number2Code, num2);
setResult(RESULT_OK, output);
finish();

Activity 1 -从前一个Activity返回将调用onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ActivityTwoRequestCode && resultCode == RESULT_OK && data != null) {
        num1 = data.getIntExtra(Number1Code);
        num2 = data.getIntExtra(Number2Code);
    }
}

更新: 回答Seenu69的评论,在活动二,

int result = Integer.parse(EditText1.getText().toString()) 
           + Integer.parse(EditText2.getText().toString());
output.putExtra(ActivityOne.KEY_RESULT, result);

在活动一中,

int result = data.getExtra(KEY_RESULT);

FirstActivity使用startActivityForResult:

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, int requestCode); // suppose requestCode == 2

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2)
    {
        String message=data.getStringExtra("MESSAGE");
    }
}

在SecondActivity上调用setResult() onClick事件或onBackPressed()

Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(Activity.RESULT_OK, intent);

有一些方法可以做到。 1. 使用startActivityForResult(),这在上面的答案中有很好的解释。

通过在你的“Utils”类或你自己的任何其他类中创建静态变量。例如,我想将studententid从ActivityB传递到ActivityA。首先,我的ActivityA调用ActivityB。然后在ActivityB中设置studententid(这是Utils.class中的一个静态字段)。 Utils.STUDENT_ID="1234";然后当回到ActivityA时,使用存储在Utils.STUDENT_ID中的studententid。 通过在应用程序类中创建getter和setter方法。

是这样的:

public class MyApplication extends Application {

    private static MyApplication instance = null;
    private String studentId="";

    public static MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public void setStudentId(String studentID){
        this.studentId=studentID;
    }

    public String getStudentId(){
        return this.studentId;
    }
}

这样就做完了。当你在ActivityB时,在里面设置数据,回到ActivityA后,获取数据。