美文网首页
Fragment中onActivityResu方法不执行的解决办

Fragment中onActivityResu方法不执行的解决办

作者: 安卓_背包客 | 来源:发表于2020-03-13 09:22 被阅读0次

需要在你要接受回调的fragment 碎片粘贴的主Activity 里面下写下一下代码

一.只嵌套了一层Fragment(比如activity中使用了viewPager,viewPager中添加了几个Fragment)

在这种情况下要注意几个点:

1.在Fragment中使用startActivityForResult的时候,不要使用getActivity().startActivityForResult,而是应该直接使startActivityForResult()。

2.如果activity中重写了onActivityResult,那么activity中的onActivityResult一定要加上super.onActivityResult(requestCode, resultCode, data)。

如果违反了上面两种情况,那么onActivityResult只能够传递到activity中的,无法传递到Fragment中的。

没有违反上面两种情况的前提下,可以直接在Fragment中使用startActivityForResult和onActivityResult,和在activity中使用的一样。



如果以上都不行就只能自己写个Activity继承FragmentActivity


package neo.door.main;

import java.util.List;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

public class MyBaseFragmentActivity extends FragmentActivity
{
    private String TAG="MyBaseFragmentActivity";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fragmentManager=getSupportFragmentManager();
        for(int indext=0;indext<fragmentManager.getFragments().size();indext++)
        {
            Fragment fragment=fragmentManager.getFragments().get(indext); //找到第一层Fragment
            if(fragment==null)
                 Log.w(TAG, "Activity result no fragment exists for index: 0x"  
                         + Integer.toHexString(requestCode));  
            else 
                handleResult(fragment,requestCode,resultCode,data);
        }
    }
    /**
     * 递归调用,对所有的子Fragment生效
     * @param fragment
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private void handleResult(Fragment fragment,int requestCode,int resultCode,Intent data) 
    {
        fragment.onActivityResult(requestCode, resultCode, data);//调用每个Fragment的onActivityResult
        Log.e(TAG, "MyBaseFragmentActivity");
        List<Fragment> childFragment = fragment.getChildFragmentManager().getFragments(); //找到第二层Fragment 
        if(childFragment!=null)
            for(Fragment f:childFragment)
                if(f!=null)
                {
                    handleResult(f, requestCode, resultCode, data);
                }
        if(childFragment==null)
            Log.e(TAG, "MyBaseFragmentActivity1111");
    }

}

Koltin()

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val fragmentManager = supportFragmentManager
        for (indext in 0 until fragmentManager.fragments.size) {
            val fragment: Fragment = fragmentManager.fragments[indext] //找到第一层Fragment
            if (fragment == null) Log.w(
                "===", "Activity result no fragment exists for index: 0x"
                        + Integer.toHexString(requestCode)
            ) else handleResult(fragment, requestCode, resultCode, data)
        }
    }

    /**
     * 递归调用,对所有的子Fragment生效
     * @param fragment
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private  fun handleResult(
        fragment: Fragment?,
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        fragment!!.onActivityResult(requestCode, resultCode, data) //调用每个Fragment的onActivityResult
        Log.e("++", "MyBaseFragmentActivity")
        val childFragment: List<Fragment> =
            fragment.childFragmentManager.fragments //找到第二层Fragment
        if (childFragment != null) for (f in childFragment) if (f != null) {
            handleResult(f, requestCode, resultCode, data)
        }
        if (childFragment == null) Log.e("=11", "MyBaseFragmentActivity1111")
    }

相关文章

网友评论

      本文标题:Fragment中onActivityResu方法不执行的解决办

      本文链接:https://www.haomeiwen.com/subject/ekefshtx.html