Android回调——Android模块化编程

在MainActivity中,启动新Activity时调的不是StartActivity()方法,而是 startActivityForResult
    
    
    
    
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SCANNIN_GREQUEST_CODE);// SCANNIN_GREQUEST_CODE是一个标识,回调识别
     
     
     
     
private final static int SCANNIN_GREQUEST_CODE = 3;// 返回扫码
private static final int CONTACT_REQUEST_CODE = 2;// 返回通讯录
在OtherActivity中,可以这样做:监听按钮,setResult
    
    
    
    
//按钮监听
private OnClickListener btnClick = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
Intent intent = getIntent();
Bundle bundle = new Bundle();
String bundleStr = numberStr;
if (bundleStr != "") {
bundleStr = bundleStr.substring(1);
}
bundle.putString("numberStr", bundleStr);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
break;
case R.id.btn_back:
finish();
break;
}
}
};
在OtherActivity中,也可以这样做:方法有很多,不一一总结:
    
    
    
    
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
 
public class TestActivity extends Activity implements Callback {
 
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
 
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
 
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
 
}

在MainActivity中,回调时这样执行:
    
    
    
    
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// super.onActivityResult(requestCode, resultCode, data);
// Toast.makeText(MainActivity.this, "进来了", Toast.LENGTH_SHORT).show();
switch (requestCode) {
case SCANNIN_GREQUEST_CODE://其他Activity A回调
if (resultCode == RESULT_OK) {
//要执行的内容
}
break;
 
case CONTACT_REQUEST_CODE://其他Activity B回调
if (resultCode == RESULT_OK) {
Toast.makeText(this, "程序B回调", Toast.LENGTH_SHORT).show();
}
break;
}
}

你可能感兴趣的:(android,回调)