java 如何从android中的活动刷新片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35286670/
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-11-02 23:57:07  来源:igfitidea点击:

How to refresh a fragment from an activity in android?

javaandroidandroid-fragmentsandroid-activity

提问by AndroidNewBee

I have a floating button in one of my fragments.On click of which an activity pops up.Now the problem is when I go back from the activity to the fragment the fragment does not auto refresh.Also I want it to refresh on back pressed. Within the fragment I have a refresh button in the action bar on pressed of which I call a refreshFragment() method.Is there a way in android that I can call a method from a fragment from within an activity. This is my refreshFragment() method code.

我的一个片段中有一个浮动按钮。单击它会弹出一个活动。现在的问题是,当我从活动返回到片段时,片段不会自动刷新。此外,我希望它在按下后刷新. 在片段中,我在按下的操作栏中有一个刷新按钮,我调用了一个 refreshFragment() 方法。在 android 中是否有一种方法可以让我从活动中的片段调用一个方法。这是我的 refreshFragment() 方法代码。

  public void refreshFragment()
    {
        Fragment fragment = new BillingFragment();
        android.support.v4.app.FragmentManager fragmentMg = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTrans = fragmentMg.beginTransaction();
        fragmentTrans.replace(R.id.container_body, fragment, "TABBILLING");
        fragmentTrans.addToBackStack(null);
        fragmentTrans.commit();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Billing");
        //adapter.notifyDataSetChanged();

    }

And I call it inside my on create view method as follows :-

我在我的创建视图方法中调用它如下:-

((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener()
        {
            @Override
            public void onRefresh() {
                refreshFragment();
            }
        });

I even tried calling displayView() method of my main activity from another activity by creating an object of MainActivity as follows:-

我什至尝试通过创建 MainActivity 对象来从另一个活动调用我的主要活动的 displayView() 方法,如下所示:-

 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                super.onBackPressed();
                MainActivity main = new MainActivity();
                main.displayView(1);
                count = 0;
                return true;

But it gave me a null pointer exception.This is my displayView() method.

但它给了我一个空指针异常。这是我的 displayView() 方法。

 public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    public void displayView(int position) {
        Fragment fragment = null;

        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                title = getString(R.string.title_home);
                break;
            case 1:
                fragment = new BillingFragment();
                title =getString(R.string.title_billing);
                break;
            case 2:
                fragment = new StockViewFragment();
                title =getString(R.string.title_stockview);
                break;
            case 3:
                fragment = new BhishiManagementFragment();
                title= getString(R.string.title_bhishiview);
                break;
            case 4:
                fragment = new ReportingFragment();
                title=getString(R.string.title_reporting);
                break;
            case 5:
                fragment = new VendorManagementFragment();
                title=getString(R.string.title_vendormanagement);
                break;
            case 6:
                fragment = new CustomerMgmt();
                title = getString(R.string.title_custmgmt);
                break;

            default:
                break;
        }

Any help is appreciated.Thank you :)

任何帮助表示赞赏。谢谢:)

回答by Guru_VL

I assume you are trying to refresh fragment when coming back from an activity (that activity does not hosting the fragment which needs to be refreshed), if my assumption is correct please try the below approach, incase not please clarify the question with more info.

我假设您在从活动返回时尝试刷新片段(该活动不托管需要刷新的片段),如果我的假设是正确的,请尝试以下方法,以防万一请用更多信息澄清问题。

Try : Have a boolean variable in fragment and update its value true or false based on fragment visible state using its lifecycle methods.

尝试:在片段中有一个布尔变量,并使用其生命周期方法根据片段可见状态更新其值 true 或 false。

public class SampleFragment extends Fragment {

    private boolean shouldRefreshOnResume = false;

    public static SampleFragment newInstance() {
        SampleFragment fragment = new SampleFragment();
        return fragment;
    }

    public SampleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, 
                                              container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Check should we need to refresh the fragment
        if(shouldRefreshOnResume){
            // refresh fragment
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        shouldRefreshOnResume = true;
    }
}