TabHost的Activity中onActivityResult方法不被调用的解决方案

项目中有用到在TabHost中嵌套子Activity,然而TabHost的子Activity中startActivityForResult调用其他Activity时候遭遇到onActivityResult方法不响应的问题.

解决办法如下:

1.通过父Activity启动其他Activity;

2.实现父Activity的onActivityResult方法,将该处接收到的请求转发给当前活动的子Activity;

3.自定义一个转发接口用来实现第2步中的转发功能;

4.子Activity中实现第3步接口中的方法用来接收返回信息;

 

代码如下:

1.通过父Activity启动其他Activity

getParent().startActivityForResult(new Intent(...), REQUEST_CODE); 

view plainco


2.实现父Activity的onActivityResult方法

@Override  
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
         // 获取当前活动的Activity实例  
         Activity subActivity = getLocalActivityManager().getCurrentActivity();  
         //判断是否实现返回值接口  
         if (subActivity instanceof OnTabActivityResultListener) {  
             //获取返回值接口实例  
             OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;  
             //转发请求到子Activity  
             listener.onTabActivityResult(requestCode, resultCode, data);  
         }  
         super.onActivityResult(requestCode, resultCode, data);  
     }  


3.自定义接口

/** 
 * 解决子Activity无法接收Activity回调的问题 
 * @author Administrator 
 * 
 */  
public interface OnTabActivityResultListener {  
    public void onTabActivityResult(int requestCode, int resultCode, Intent data);  
} 

4.子Activity需要实现onTabActivityResult方法

@Override  
    public void onTabActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {  
            //实现该处逻辑  
        ...  
        }  
    }  


注意子Activity一定要实现implements   OnTabActivityResultListener 

然后重写

public void onTabActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case SUB_REQUEST_CODE:  
                //Sub_History_visit();  
                break;  
  
            default:  
                break;  
            }  
        }  
    }  



参考:http://www.johdan.com/tabhost_do_not_-exist-_startactivityforresult.html

你可能感兴趣的:(TabHost的Activity中onActivityResult方法不被调用的解决方案)