在Fragment中的Activity——startActivityForResult与onActivityResult

fragmanager = this.getSupportFragmentManager();
FragmentTransaction transaction = fragmanager.beginTransaction();
tabHost = new FragMapOList();
transaction.add(R.id.fragment_map_list, tabHost, "f1");
transaction.show(tabHost);
transaction.commit();
 
tabHost继承了Fragment类
在onCreateView方法中做了如下处理
Intent mapIntent = new Intent();
View btnmap = mInflater.inflate(R.layout.frag_btn_map_layout, null);
mapIntent.setClass(getActivity(), MapActivity.class);
Intent listIntent = new Intent();
View btnlist = mInflater.inflate(R.layout.frag_btn_list_layout, null);
listIntent.setClass(getActivity(), DSListActivity.class);


host.addTab(host.newTabSpec("tab1").setIndicator(btnmap).setContent(mapIntent));
host.addTab(host.newTabSpec("tab2").setIndicator(btnlist).setContent(listIntent));

那么问题来了,如果我在MapActivity中用startActivityForResult(MapActivity.this, xxx.class)方法,并且在这某某activity中setResult了,然后再在其中用onActivityResult能接收到吗?
并不能!
那么在Fragment或是父Activity中用onActivityResult呢?
然并卵!
可选择通过startActivityForResult(MapActivity.this.getParent(), xxx.class),让后在父Activity中用onActivityResult来做相应处理。
那么如果是activity.sendBroadcast(intent);发送广播和接收广播呢?
我们先看看host.newTabSpec("tab1").setIndicator(btnmap).setContent(mapIntent)做了什么
方法newTabSpec实例化了一个内部类TabSpec,其方法setContent
/**
 * Specify an intent to use to launch an activity as the tab content.
 */
public TabSpec setContent(Intent intent) {
    mContentStrategy = new IntentContentStrategy(mTag, intent);
    return this;
}
其中new IntentContentStrategy(mTag, intent)实例化另一个内部类,我们在切换tab1和tab2的时候,这个类的getContentView()方法将被调用并返回View,其中
mLocalActivityManager.startActivity(mTag, mIntent)只是用主线程对象装载了Activity,并没有启动;所以最后就相当于放了个View进到tab中而已,这就解
释了上面onActivityResult为什么没反应。那么广播究竟可不可以在其中被接收呢?你自己可以尝试一下。
public View getContentView() {
    if (mLocalActivityManager == null) {
        throw new IllegalStateException("Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?");
    }
    final Window w = mLocalActivityManager.startActivity(
            mTag, mIntent);
    final View wd = w != null ? w.getDecorView() : null;
    if (mLaunchedView != wd && mLaunchedView != null) {
        if (mLaunchedView.getParent() != null) {
            mTabContent.removeView(mLaunchedView);
        }
    }
    mLaunchedView = wd;

    // XXX Set FOCUS_AFTER_DESCENDANTS on embedded activities for now so they can get
    // focus if none of their children have it. They need focus to be able to
    // display menu items.
    //
    // Replace this with something better when Bug 628886 is fixed...
    //
    if (mLaunchedView != null) {
        mLaunchedView.setVisibility(View.VISIBLE);
        mLaunchedView.setFocusableInTouchMode(true);
        ((ViewGroup) mLaunchedView).setDescendantFocusability(
                FOCUS_AFTER_DESCENDANTS);
    }
    return mLaunchedView;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值