gpt4 book ai didi

android - 为什么在隐藏的 fragment 中调用 onResume()?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:18 25 4
gpt4 key购买 nike

我的应用在主屏幕上显示了很多图像。用户可以通过触摸图像查看有关产品的更多信息。主屏幕 fragment 被隐藏,产品详细信息 fragment 变得可见。通过单击后退键,主屏幕 fragment 再次变得可见。

fragment 事务实现如下:

    @Override
public void showProduct(Product p, boolean isParentTabbed) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();

// the new fragment
Fragment mFragment = new ProductDetailFragment(p,isParentTabbed);

//hide main screen fragment and add product detail fragment
transaction.hide(currentlyOpenedFragment);
transaction.add(android.R.id.content,mFragment);

//set new fragment as current "on top" fragment
currentlyOpenedFragment = mFragment;

//start animation
transaction.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_top);

transaction.addToBackStack(null);
transaction.commit();
}

一切正常,除非用户在产品详细信息 fragment 中打开共享对话框(标准 android 共享 Intent )并通过单击后退键关闭对话框。由于某种原因,调用了主屏幕 fragment (隐藏)中的 onResume 方法。我通过将以下代码添加到主屏幕 fragment 中的 onResume 方法解决了这个问题:

    super.onResume();
if(this.isHidden()){
Log.d("tab","dont resume tab0fragment because it is hidden");
return;
}

这工作正常,但问题仍然存在:当用户关闭其他 fragment 中的共享对话框时,为什么在隐藏 fragment 中调用 onResume()?

最佳答案

隐藏 fragment 仍然遵循 fragment 生命周期。查看来自 documentation 的流程图. User navigates backwards or the fragment is removed/replaced.导致 onDestroyView()被调用,其中 The fragment returns to the layout from the back stack,这是您的主屏幕 fragment 所在的位置。

关于android - 为什么在隐藏的 fragment 中调用 onResume()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com