【startActivityForResult】Android Activity回传数据

Android用Intent来做activity的跳转,轻量级的数据用Bundle配合着传递。

传回数据用startActivityForResult来搞。

跳转时代码:


_Intent=newIntent();
_Intent.setClass(getBaseContext(), Appearance.class);
startActivityForResult(_Intent,1);


重载方法:


@Override
protectedvoid onActivityResult(intrequestCode, intresultCode, Intent data) {
    Bundle _Bundle;
    switch(requestCode) {
        case1:
            if(resultCode == RESULT_OK) {
                _Bundle = data.getExtras();
                String NAME= _Bundle.getString("NAME");
            }
            break;
        default:
            break;
    }
}


另一个activity回传:


Intent _Intent = new Intent();
Bundle _Bundle = new Bundle();
_Bundle.putString("NAME","LINCYANG");
_Intent.putExtras(_Bundle);
Appearance.this.setResult(RESULT_OK,_Intent);
Appearance.this.finish();



你可能感兴趣的:(安卓基础Base,Android)