多个活动返回数据到主活动

huangapple go评论68阅读模式
英文:

Data return from Multiple Activities to Main Activity

问题

大家好。我已经开始编写(1个月大)Android应用程序,对于这方面的知识我还很陌生。

我有一个名为X的活动。在该活动X上,我有四个不同的按钮,分别调用了四个不同的活动。在这些三个活动中,我有一个按钮,并且在按下按钮后,我会将数据返回给活动X。此外,在活动X上,我可能不会调用这三个活动中的所有三个。我可能只调用1、3,或者只调用2、3。我已经搜索了很多,但是我找不到我寻求的内容。

编辑:当我在活动1、2、3中使用finish();时,我可以调用其中一个活动(假设调用了活动1),并且能够获取数据并将其返回到活动X,但是当我尝试调用另一个活动(假设调用了活动2)时,我可以进入该活动,但是无法从它返回,因为它不知道要返回到哪里。

以下是我寻求的解决方案:

活动X -> 第一个活动 -> 返回数据1到X

活动X -> 第二个活动 -> 返回数据2到X

活动X -> 第三个活动 -> 返回数据3到X

活动X -> 发送数据1 + 数据2 + 数据3到第四个活动

我在活动X中通过onClick方法启动了我的三个活动(data_globalActivity X类中是全局变量)

switch (view.getId()) {
    case R.id.btn1:
        Intent i = new Intent(this, Activity1.class);
        startActivityForResult(i, 200);
        break;
    case R.id.btn2:
        i = new Intent(this, Activity2.class);
        startActivityForResult(i, 201);
        break;
    case R.id.btn3:
        i = new Intent(this, Activity3.class);
        startActivityForResult(i, 202);
        break;
    case R.id.btn4:
        i = new Intent(this, Activity4.class);
        i.putExtra("Extra", data_global);
        startActivity(i);
        break;
}

我在Activity X中的onActivityResult方法中获取数据:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case 200:
                String extra1 = data.getStringExtra("Extra1");
                if (extra1 != null && !extra1.isEmpty()) {
                    data_global += extra1;
                }
                break;
            case 201:
                String extra2 = data.getStringExtra("Extra2");
                if (extra2 != null && !extra2.isEmpty()) {
                    data_global += extra2;
                }
                break;
            case 202:
                String extra3 = data.getStringExtra("Extra3");
                if (extra3 != null && !extra3.isEmpty()) {
                    data_global += extra3;
                }
                break;
        }
    }
}

而活动1、2或3中的代码如下所示:(X在Extra中指代1、2或3)

case R.id.btnSave:
    Intent resultIntent = new Intent();
    resultIntent.putExtra("ExtraX", "name");
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
    break;

我该如何解决这个问题?非常感谢。

英文:

everyone. I have started to write(1-month-old) Android apps and I am pretty new this stuff.

I have a activity X. On that activity X, I have four different buttons calls four different activities. On those three activities, I have button, and I am returning data to activity X after I pressed button. Also, on the activity X, I may not call all three of the activies. I might call just 1,3 or just 2,3. I have looked a lot but I could not find what I seek for.

EDIT: when I put finish();to activity 1,2,3, I could call one of the activies(assume activity one called) and able to grab data, and returns the Activity X, but when I started to call other one (assumed activity 2 called), I could go to this activity, but I cannot return from it since it does know where to return.

This is what I seek for:

Activity X-> 1st activity -> return data1 to X

Activity X-> 2nd activity -> return data2 to X

Activity X-> 3rd activity -> return data3 to X

Activity X-> send data1 + data2 +data3 to 4th activity

I started my three activities on Activity X like this under onClick method
(data_global is global on Activity X class)

case R.id.btn1:
       Intent i = new Intent(this, Activity1.class);
       startActivityForResult(i, 200);
       break;
case R.id.btn2:
       i = new Intent(this, Activity2.class);
       startActivityForResult(i, 201);
       break;
case R.id.btn3:
       i = new Intent(this, Activity3.class);
       startActivityForResult(i, 202);
       break;
case R.id.btn4:
       i = new Intent(this, Activity4.class);
       i.putExtra("Extra", data_global);
       startActivity(i);
       break;

I take my data from here on Activity X under onActivityResult method:

switch (requestCode) {
        case (200): {
            if (resultCode == Activity.RESULT_OK) {
                if (data.getStringExtra("Extra1").length() != 0) {
                    data_global += data.getStringExtra("Extra1");
                }
            }
            break;
        }
        case (201): {
            if (resultCode == Activity.RESULT_OK) {
                if (data.getStringExtra("Extra2").length() != 0) {
                    data_global += data.getStringExtra("Extra2");
                }
            }
            break;
        }
        case (202): {
            if (resultCode == Activity.RESULT_OK) {
                if (data.getStringExtra("Extra3").length() != 0) {
                    data_global += data.getStringExtra("Extra3");
                }
            }
            break;
        }

And any of activity 1 or 2 or 3 code is like that:(X is at the end on Extra referring to 1,2,3)

case R.id.btnSave:
            Intent resultIntent = new Intent();
            resultIntent.putExtra("ExtraX", "name");
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
            break;

How can I solve my problem? Many thanks.

答案1

得分: 1

你需要在每个活动的意图中指定当前活动和目标活动,如下所示:

case R.id.btnSave:
    Intent resultIntent = new Intent(Activity1.this, MainActivity.class);

并且对于其他活动也要重复这样做。

英文:

You need to specify the current and destination activity in your intent in each activity as below

case R.id.btnSave:
    Intent resultIntent = new Intent(Activity1.this, MainActivity.class);

and repeat that for the other activities.

huangapple
  • 本文由 发表于 2020年4月11日 07:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61150070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定