多个Ativity值传递FLAG_ACTIVITY_FORWARD_RESULT

需求场景为A——>B——>C 三个跳转
1.a将数据交给b处理
2.b处理完数据(finish())
3.b处理完数据 结果交给c来处理
数据处理中间层开启(B)activity要加上

intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

FLAG_ACTIVITY_FORWARD_RESULT主要作用:
如果设置,并且这个Intent用于从一个存在的Activity启动一个新的Activity,那么,这个作为答复目标的Activity将会传到这个新的Activity中。这种方式下,新的Activity可以调用setResult(int),并且这个结果值将发送给那个作为答复目标的Activity。

link:Flags列表

A界面

/** * Created by qinwei on 2016/3/3 17:44 * email:[email protected] */
public class AActivity extends BaseActivity {
    @Override
    protected void setContentView() {
        setContentView(R.layout.a0);
    }

    @Override
    protected void initializeData() {

    }

    public void goB(View v){
        Intent intent=new Intent(this,BActivity.class);
        startActivityForResult(intent,200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            TextView mLabel = (TextView) findViewById(R.id.mLabel);
            mLabel.setText(data.getStringExtra("data"));
        }
    }
}

B界面


/** * Created by qinwei on 2016/3/3 17:44 * email:[email protected] */
public class BActivity extends BaseActivity {
    @Override
    protected void setContentView() {
        setContentView(R.layout.a1);
    }

    @Override
    protected void initializeData() {

    }
    public void goC(View v){
        Intent intent=new Intent(this,CActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        startActivity(intent);
        finish();
    }
}

C界面

/** * Created by qinwei on 2016/3/3 17:44 * email:[email protected] */
public class CActivity extends BaseActivity {
    @Override
    protected void setContentView() {
        setContentView(R.layout.a2);
    }

    @Override
    protected void initializeData() {

    }

    public void goA(View v){
        Intent intent=new Intent();
        EditText editText = (EditText) findViewById(R.id.editText);
        intent.putExtra("data",editText.getText().toString().trim());
        setResult(RESULT_OK,intent);
        finish();
    }
}

你可能感兴趣的:(多个Ativity值传递FLAG_ACTIVITY_FORWARD_RESULT)