onActivityResult在片段不叫 [英] onActivityResult not call in the Fragment
问题描述
应用程序的结构是这样的:
The structure of the app is like this:
tabHost (in Activity) -> contains -> TabFragment(extend base container fragment)
1。在code。在活动
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.btn_home)),
HomeFragment.class, null);
2。在code在HomeFragment (请注意,HomeFragment是不实际的功能,但这样的容器中,并将其扩展BaseContainerFragment):
2. The code in HomeFragment (Notice that HomeFragment is not the actual function but a container like this, and it extend BaseContainerFragment):
public class HomeFragment extends BaseContainerFragment {
public Home homeFrag;
private boolean mIsViewInited;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.container_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
private void initView() {
homeFrag = new Home();
replaceFragment(homeFrag, false);
}
}
3。 BaseContainerFragment
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
}
public boolean popFragment() {
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
4。在主页(该片段的实际内容)
UploadType fragment = new UploadType();
Bundle bundle = new Bundle();
bundle.putString("form_type", "request");
fragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
5。而在UploadType,我调用摄像头活动,但onActivityResult只返回的主要活动。
startActivityForResult(intent, REQUEST_CAMERA);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("test1", "result2");
super.onActivityResult(requestCode, resultCode, data);
}
我怎样才能触发onActivityResult在UploadType?感谢您的帮助。
How can I trigger the onActivityResult at UploadType? Thanks for help.
推荐答案
为什么这不工作的原因是因为您正在呼叫 startActivityForResult()
在嵌套分段。 Android是足够聪明的航线将结果返回给一个活动
甚至片段
,而不是一个嵌套的片段
因此,为什么你没有得到回调。
(更多信息,为什么不在这里工作了 或<一href="https://stackoverflow.com/questions/13580075/onactivityresult-not-called-in-new-nested-fragment-api">stackoverflow)
The reason why this doesn't work is because you are calling startActivityForResult()
from within a nested fragment. Android is smart enough to route the result back to an Activity
and even a Fragment
, but not to a nested Fragment
hence why you don't get the callback.
(more information to why that doesn't work here or on stackoverflow)
现在,为了使它工作,我建议你手动航线回调到 ChildFragment
(= UploadType
)在 ParentFragment
(= BaseContainerFragment
):
Now in order to make it work I suggest you manually route the callback to the ChildFragment
(=UploadType
) in the ParentFragment
(=BaseContainerFragment
):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment uploadType = getChildFragmentManager().findFragmentById(R.id.container_framelayout);
if (uploadType != null) {
uploadType.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
这篇关于onActivityResult在片段不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!