Android 重新加载时刷新片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20702333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:18:35  来源:igfitidea点击:

Refresh Fragment at reload

androidandroid-fragments

提问by Samantha Withanage

In an android application I'm loading data from a Db into a TableViewinside a Fragment. But when I reload the Fragmentit displays the previous data. Can I repopulate the Fragmentwith current data instead of previous data?

在Android应用程序我加载从DB数据到TableView里面Fragment。但是当我重新加载Fragment它时,它会显示以前的数据。我可以Fragment用当前数据而不是以前的数据重新填充吗?

回答by Phant?maxx

I think you want to refresh the fragment contents upon db update

我想你想在数据库更新时刷新片段内容

If so, detach the fragment and reattach it

如果是这样,请分离片段并重新附加它

// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Your_Fragment_TAG is the name you gave your fragment when you created it

Your_Fragment_TAG 是您在创建片段时为其指定的名称

This code is for support library.

此代码用于支持库。

If you're not supporting older devices, just use getFragmentManagerinstead of getSupportFragmentManager

如果您不支持旧设备,只需使用getFragmentManager而不是getSupportFragmentManager

[EDIT]

[编辑]

This method requires the Fragment to have a tag.
In case you don't have it, then @Hammer's method is what you need.

此方法要求 Fragment 具有标记。
如果您没有它,那么@Hammer 的方法就是您所需要的。

回答by Mbengue Assane

This will refresh current fragment :

这将刷新当前片段:

FragmentTransaction ft = getFragmentManager().beginTransaction();
if (Build.VERSION.SDK_INT >= 26) {
   ft.setReorderingAllowed(false);
}
ft.detach(this).attach(this).commit();

回答by Hammer

In case you do not have the fragment tag, the following code works well for me.

如果您没有片段标签,以下代码对我来说效果很好。

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
if (currentFragment instanceof "NAME OF YOUR FRAGMENT CLASS") {
 FragmentTransaction fragTransaction =   (getActivity()).getFragmentManager().beginTransaction();
 fragTransaction.detach(currentFragment);
 fragTransaction.attach(currentFragment);
 fragTransaction.commit();}
}

回答by Kaushik Khambhadiya

you can refresh your fragment when it is visible to user, just add this code into your Fragment this will refresh your fragment when it is visible.

您可以在用户可见时刷新您的片段,只需将此代码添加到您的 Fragment 中,这将在您可见时刷新您的片段。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // Refresh your fragment here          
  getFragmentManager().beginTransaction().detach(this).attach(this).commit();
        Log.i("IsRefresh", "Yes");
    }
}

回答by Sachin Jagtap

To refresh the fragment accepted answer will not work on Nougat and above version. To make it work on all os you can do following.

刷新片段接受的答案不适用于牛轧糖及以上版本。要使其在所有操作系统上运行,您可以执行以下操作。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        fragmentManager.beginTransaction().detach(this).commitNow();
        fragmentManager.beginTransaction().attach(this).commitNow();
    } else {
        fragmentManager.beginTransaction().detach(this).attach(this).commit();
    }

回答by Ali Nem

You cannot reload the fragment while it is attached to an Activity, where you get "Fragment Already Added" exception.

当片段附加到活动时,您无法重新加载片段,在那里您会收到“ Fragment Already Added”异常。

So the fragment has to be first detached from its activity and then attached. All can be done using the fluent api in one line:

因此片段必须首先从其活动中分离,然后再附加。所有这些都可以在一行中使用 fluent api 完成:

getFragmentManager().beginTransaction().detach(this).attach(this).commit();

Update:This is to incorporate the changes made to API 26 and above:

更新:这是为了合并对 API 26 及更高版本所做的更改:

FragmentTransaction transaction = mActivity.getFragmentManager()
                        .beginTransaction();
                if (Build.VERSION.SDK_INT >= 26) {
                    transaction.setReorderingAllowed(false);
                }
                transaction.detach(this).attach
                        (this).commit();

For more description of the update please see https://stackoverflow.com/a/51327440/4514796

有关更新的更多说明,请参阅https://stackoverflow.com/a/51327440/4514796

回答by Irshu

   MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        getSupportFragmentManager().beginTransaction().detach(fragment).attach(fragment).commit();

this will only work if u use FragmentManagerto initialize the fragment. If u have it as a <fragment ... />in XML, it won't call the onCreateViewagain. Wasted my 30 minutes to figure this out.

这仅在您FragmentManager用于初始化片段时才有效。如果你把它作为<fragment ... />XML 格式,它就不会再调用了onCreateView。浪费了我 30 分钟来弄清楚这一点。

回答by hardiBSalih

Here what i did and it worked for me i use firebase and when user is logIn i wanted to refresh current Fragment first you will need to requer context from activity because fragment dont have a way to get context unless you set it from Activity or context here is the code i used and worked in kotlin language i think you could use the same in java class

在这里,我所做的和它对我有用,我使用 firebase,当用户登录时,我想首先刷新当前 Fragment,您需要从活动中重新获取上下文,因为 Fragment 无法获取上下文,除非您在此处从 Activity 或上下文中设置它是我在 kotlin 语言中使用和工作的代码,我认为您可以在 java 类中使用相同的代码

   override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)
    val context = requireActivity()
    if (auth.currentUser != null) {
        if (isVisibleToUser){
            context.supportFragmentManager.beginTransaction().detach(this).attach(this).commit()
        }
    }

}

回答by Mwangi Gituathi

Make use of onResume method... both on the fragment activity and the activity holding the fragment.

在片段活动和持有片段的活动中使用 onResume 方法。

回答by Brayan Steven Martínez Vanegas

getActivity().getSupportFragmentManager().beginTransaction().replace(GeneralInfo.this.getId(), new GeneralInfo()).commit();

GeneralInfoit's my Fragment class GeneralInfo.java

GeneralInfo这是我的片段类 GeneralInfo.java

I put it as a method in the fragment class:

我把它作为一个方法放在片段类中:

public void Reload(){
    getActivity().getSupportFragmentManager().beginTransaction().replace(LogActivity.this.getId(), new LogActivity()).commit();
}