简体   繁体   中英

How to Refresh the fragment, when Internet Connection is Resume

I had a fragment, in which first I check for availability for internet connection. If its available its fine, otherwise, I want to refresh the page by clicking on retry button. The problem which comes is, I am not able to refresh the fragment This is my fragment code.

public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.homework, container, false);

            listView = rootView.findViewById(R.id.home_list);

            adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);

            listView.setAdapter(adapter);

            cal = rootView.findViewById(R.id.date_select);



        boolean x = handleNetworkConnection();
        if (x == true) {
            methodListener();
            SharedPreferences preferences = getActivity().getSharedPreferences("user_login", Context.MODE_PRIVATE);

            HashMap<String, String> map = new HashMap<>();
            map.put("school_id", preferences.getString("school_id", ""));
            map.put("class", preferences.getString("classes", ""));
            map.put("sec", "homeera");

            defaultfetch(map);

        }
        else
        {
             final LinearLayout ll = new LinearLayout(getActivity());
            TextView textView = new TextView(getActivity());
            Button button = new Button(getActivity());

            ImageView image = new ImageView(getActivity());
            ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            ll.setBackgroundColor(Color.parseColor("#ecf0f1"));
            image.setImageResource(R.drawable.errorsq);
            button.setBackgroundColor(Color.parseColor("#02b48a"));

            button.setText("Try Again");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
/*
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.detach(getActivity().).attach(this).commit();
*/

                }
            });
            ll.addView(textView);
            ll.addView(image);
            ll.addView(button);
            ll.setOrientation(LinearLayout.VERTICAL);




            View rootview = ll;

            return rootview;
        }

            return rootView;
        }

In this code, I had applied a way to refresh the fragment, but the problem is that after refreshing UI comes blank. Nothing is there on the screen.

You should not refresh the fragment but refresh the list when you get an internet connection.

Since your method handleNetworkConnection() returns synchronously, you could organize the code as follows:

public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.homework, container, false);
        listView = rootView.findViewById(R.id.home_list);
        adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
        listView.setAdapter(adapter);
        cal = rootView.findViewById(R.id.date_select);

        prepareList();
 }

 void prepareList() {
     boolean x = handleNetworkConnection();
    if (x == true) {
       // same code as now, load the list content and display it
    } else {
       // same code as now except the click listener:
           public void onClick(View view) {
                prepareList();
            }
       //
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM