StartActivityForResult

StartActivity

用startActivityForResult(intent, requestcode);启动子功能模块activity

onActivityResult(int requestCode, int resultCode, Intent data)对子activity返回进行判断处理

public class StartActivity extends Activity implements OnClickListener {
    private Button bt;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        bt = (Button) findViewById(R.id.bt);
        tv = (TextView) findViewById(R.id.tv);
        bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent(StartActivity.this, RequestActivity.class);
        startActivityForResult(i, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 1 && resultCode == RESULT_OK) {
            tv.setText(data.getStringExtra("request"));
        }
    }

RequestActivity

setResult(resuletCode,Intent);intent里放要返回的数据

finish();返回父activity;

public class RequestActivity extends Activity implements OnClickListener {
    private EditText et;
    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.request);
        et = (EditText) findViewById(R.id.et);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent();
        i.putExtra("request", et.getText().toString());
        setResult(RESULT_OK, i);
        finish();
    }

}

 

posted @ 2012-12-05 11:24  Mr.haox  阅读(288)  评论(0编辑  收藏  举报