0

My android app requires internet connectivity. I have used broadcastreceiver to monitor the status of network connectivity. But, how do I pause the activity when the network connectivity goes off?

  • You can use ProgressDialog/AlertDialog to display a message like "Waiting for internet connection" that will wrap the whole view, also you need to set dialog.setCancelable(false) so that it wont disappear when user click on screen – Muhammad Husnain Tahir Nov 15 '16 at 13:11

2 Answers2

0

If you get the status of the network connectivity, when it is not connected just call your Activity's onPause method?

public void onPause() {
    super.onPause();
    //pause anything else
}

then call onResume when you have network again?

public void onResume() {
    super.onResume();
    //resume application
}
Nathan Headley
  • 152
  • 1
  • 13
0

It depends on what you mean by pause the activity:

  • If you want to prevent the user from doing anything on the activity when there is no internet then you should show a no network view and hide everything else
  • If you want to actually trigger the on pause on that activity I think that can only happen when you move to another activity
  • You can also finish the activity and show a toast message that you can't work without an internet connection

Please provide more clarification I might be able to help

  • UPDATE:

Based on your comment, you can show a view with a retry button and hide the ListView

You can also make use of ListView.setEmptyView(View), have a look at this StackOverflow post about that setEmptyView on ListView not showing its view in a android app

But I usually create something like that inside a RelativeLayout:

`

<LinearLayout
    android:id="@+id/layoutNoInternet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Please check your internet connection!" />

    <Button
        android:id="@+id/btnRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Retry"/>
</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

`

I set the visibility of no internet layout to gone or visible

Community
  • 1
  • 1
Mohammad Abbas
  • 245
  • 2
  • 13
  • My application has two listview activities which gets populated based on the response from webservice. So, when there is no network connection, I would like to show a Toast message and hide everything else giving an option for the user to retry. – user7161912 Nov 16 '16 at 03:52