应用程序的结构如下:
tabHost (in Activity) -> contains -> TabFragment(extend base container fragment)
1。活动中的代码:
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.btn_home)),
HomeFragment.class, null);
2。 HomeFragment中的代码 (请注意,HomeFragment不是实际的函数,而是像这样的容器,它扩展了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);
}
如何在UploadType上触发onActivityResult?谢谢你的帮助。
答案 0 :(得分:30)
这不起作用的原因是因为您在嵌套片段中调用startActivityForResult()
。 Android非常智能,可以将结果路由回Activity
甚至是Fragment
,但不会回到嵌套Fragment
,因此您无法获得回调。
(有关为什么不起作用的更多信息here或stackoverflow)
现在为了使其正常工作,我建议您手动将回调路由到ChildFragment
(= UploadType
)中的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);
}
答案 1 :(得分:13)
就我而言,我已在0 || 1
250 || 2
...
MainActivity.java
答案 2 :(得分:8)
在你的活动覆盖onActivityForResult()中,像这样
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
现在在你的片段中,你可以在这个
中获得活动结果@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("test1", "result2");
}
确保当你正在调用启动ActivityForResult时,它应该像这样
startActivityForResult(intent, REQUEST_CAMERA);
答案 3 :(得分:0)
TabActivity-> ActivityA-> FragmentB,它不起作用。
使用坏的坏方法:
ActivityA.java
public void onSelectSomething(){
...
startActivityForResult(intent, 22222);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mFragment != null) {
mFragment.onActivityResult(requestCode, resultCode, data);
}
}
FragmentB.java
if(getActivity() instanceof ActivityA) {
((RepairerListActivity)getActivity()).onSelectSomething();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 22222) {
// do things
}
}
答案 4 :(得分:0)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Loop through all child fragments of the activity
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
// check if the current fragment is YourFragment
if (fragment instanceof YourFragment ) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
答案 5 :(得分:0)
如果您正在使用单个活动,并且在NavHostFragment
内有片段,则存在onActivityResult()
的子片段NavHostFragment
未被调用的问题。
要解决此问题,您需要从主机活动的onActivityResult()
内部手动调用子片段的onActivityResult()
。托管活动是托管您的NavHostFragment
的活动。
以下是您的主机活动onActivityResult()
的Kotlin代码:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment)
val childFragments = navHostFragment?.childFragmentManager?.fragments
childFragments?.forEach { it.onActivityResult(requestCode, resultCode, data) }
}