Android - 在两个活动之间使用putExtras传递几个参数

时间:2016-11-09 00:21:59

标签: android

MainActivityChangeBrush之间传递画笔大小和画笔形状。

主:

static final int ACTIVITY_BRUSH_SIZE_REQUEST_CODE = 2;
static final int ACTIVITY_BRUSH_SHAPE_REQUEST_CODE = 3;

public void onClickBrush(View view) {
    Intent intent = new Intent(MainActivity.this, ChangeBrush.class);

    startActivityForResult(intent, ACTIVITY_BRUSH_SIZE_REQUEST_CODE);
    startActivityForResult(intent, ACTIVITY_BRUSH_SHAPE_REQUEST_CODE);
}

ChangeBrush:

 public void onClickChangeBrushSize(View view) {
    String size = view.getTag().toString().;

    // return the brush size to main activity
    Bundle bundle = new Bundle();
    bundle.putString("size", size);

    Intent intent = new Intent();
    intent.putExtras(bundle);
    setResult(Activity.RESULT_OK, intent);

    finish();
}

public void onClickChangeBrushShape(View view) {
    String shape = view.getTag().toString();

    // return the brush shape to main activity
    Bundle bundle = new Bundle();
    bundle.putString("shape", shape);

    Intent intent = new Intent();
    intent.putExtras(bundle);
    setResult(Activity.RESULT_OK, intent);

    finish();
}

SelectBrush活动布局:

enter image description here

当用户点击size活动中的shape按钮和ChangeBrush按钮时,他们可以将sizeshape传递给MainActivity。在MainActivity中,我使用onActivityResult来设置画笔大小和形状。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        Bundle bundle = data.getExtras();
        String color = bundle.getString("color");
        String size = bundle.getString("size");
        String shape = bundle.getString("shape");

        switch(requestCode) {
            case ACTIVITY_COLOR_REQUEST_CODE:
                fingerPainterView.setColour(Color.parseColor(color)); // set new color value

            case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
                Log.d("FingerPainter", "Main activity change brush size to " + size);

            case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
                Log.d("FingerPainter", "Main activity change brush shape to " + shape);
        }

    } else if(resultCode == RESULT_CANCELED) {
        Log.d("FingerPainter", "MainActivity canceled");

    }
}

这将记录返回值:

D/FingerPainter: Main activity change brush shape to null
D/FingerPainter: Main activity change brush size to null
D/FingerPainter: Main activity change brush shape to SQUARE

似乎返回值可能对应于正确的意图。我不知道为什么它会两次记录形状信息。 finish()有什么问题吗?我希望用户选择大小和形状,然后返回主活动。

如果方法错误,我应该怎么做才能传递这两个值?

2 个答案:

答案 0 :(得分:0)

我看到两个问题:

1:您正在拨打startActivityForResult两次。您可以使用intent.putExtra(...)传递附加信息。

2:您的break声明中没有switch

switch(requestCode) {
    case ACTIVITY_COLOR_REQUEST_CODE:
        fingerPainterView.setColour(Color.parseColor(color)); // set new color value
    break;
    case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
        Log.d("FingerPainter", "Main activity change brush size to " + size);
    break;
    case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
        Log.d("FingerPainter", "Main activity change brush shape to " + shape);
    break;
}

答案 1 :(得分:0)

如果您尝试从单个Intent更新多个内容,我认为您需要保存活动中已存在的当前数据。

public void onClickBrush(View view) {
    Intent intent = new Intent(MainActivity.this, ChangeBrush.class);
    // intent.putExtra("shape", currentShape); // for example 

    // then, only start one Activity, with a generic request code, not one Activity twice with two codes 

换句话说,您启动一​​个能够更改所有数据的活动(即使您一次只更新一个值)。因此,在启动下一个Activity时保存当前值,在更新新值时将这些值保持不变。那你就不应该得到空值。

例如,对于形状按钮。

public void onClickChangeBrushShape(View view) {
    String shape = view.getTag().toString();

    // return the brush shape to main activity
    Bundle bundle = new Bundle();

    bundle.putString("shape", shape); // Set the new shape

    bundle.putString("color", color); // I assume you've saved this
    bundle.putString("size", size);  // and this...

    // You could put string extras into the intent directly, but whatever
    Intent intent = new Intent();
    intent.putExtras(bundle);
    setResult(Activity.RESULT_OK, intent);

    finish();
}

另一种选择是使用SharedPreferences存储所有值,而忘记Intents和Bundles。