0

I have a Main Activity, which holds 1 fragment. The fragment is responsible for drawing the UI, running an Async task, etc. All of this requires an internet connection. Now when I first launch my app I check whether there is an internet connection or not through a method:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting());
}

If there is no network connection, the activity starts the fragment, but I've made it so that without an internet connection nothing shows (since there is nothing to show because I am downloading content from an online database).

I want to implement a broadcast receiver, which would restart the fragment somehow, when there is an internet connection available. So far I have a broadcast receiver as an Inner class in my Main activity:

private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, final Intent intent) {

                if (intent.getExtras() != null) {
                    final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

                    if (ni != null && ni.isConnectedOrConnecting()) {
                        Toast.makeText(context, "internet ++", Toast.LENGTH_LONG).show();

                        //this is where the fragment needs to be somehow reinstantiated

                    } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                        Toast.makeText(context, "internet --", Toast.LENGTH_LONG).show();

                    }
                }
            }
        };

I've tried to make the broadcast receiver an outer class, but then I cannot do anything to the fragment.. When it is an Inner class, nothing happens with the code from the broadcast receiver. I've reviewed a lot of similar questions, but nothing seems to work from me.

So the question at hand would be: How do I refresh a fragment inside an activity, when the internet connection becomes available while the app is running?

Mikas
  • 45
  • 1
  • 3
  • You don't need to Broadcast Receiver if the Fragment is attached to the Hosting Activity, you can get your fragment through FragmentManager (v4 - getSupportFragmentManager) and just call a method to redraw the UI by casting to your Fragment type. I.e `((MyFragment)getSupportFragmentManager.getFragmentByTag(fragmentTag)).updateUI();` - syntax maybe slightly wrong .. not in front of computer. – Mark Dec 03 '16 at 13:15
  • Please refer to this solution http://stackoverflow.com/questions/25215878/how-to-update-the-ui-of-activity-from-broadcastreceiver – sup4eli Dec 03 '16 at 13:24
  • @sup4eli thanks, I'll try to figure something out.. – Mikas Dec 03 '16 at 13:47

1 Answers1

-1

Your implementation is just wrong. You should start your fragment and request through the network, and meanwhile show ProgressBar or something else to the user. If Internet is not available, in any way, you get the error then show user the problem. On the other hand, if you get the response successfully, just set your data to your view. BTW, your method for finding availability of network is not guaranteed that "Internet" is available.

hadilq
  • 1,023
  • 11
  • 25
  • Disagree on the implementation since we don't have the full picture about what the whole app is meant to do. Second of all, if you think @Mikas method is not suitable, specify why and maybe suggest an improvement. – sup4eli Dec 03 '16 at 13:22
  • I already told why! because "availability of network is not guaranteed that "Internet" is available". And I just suggested a better implementation! – hadilq Dec 03 '16 at 13:27
  • I meant different implementation to check network connection status. – sup4eli Dec 03 '16 at 13:32
  • Nothing will work correctly and accurately until you really make a request and get a successful response. That's why people implement apps just like what I suggested. – hadilq Dec 03 '16 at 13:36