Reputation: 2624
I have a very common problem which I think every android developer faces at some time. I am making an app which loads data from web-server. So internet connectivity is being used. But I have to check for internet connectivity before every call to the server. I am doing it using ConnectivityManager and it works.
My problem is actually about notifying user if the internet connectivity is lost. I want to show the user an AlertDialog box telling him about lost internet connection. Now the problem is that if user clicks on a button and a new Activity has to be started(which will load data from server), then I am not able to show him the dialog box on the calling Activity . The catch here is that I don't want to navigate to other activity if internet is not available. I just want to remain on this activity.
There are a lot of buttons on this activity and on clicking them , user can go to different activities. All I want is not to open other activity if no internet is available and show him the dialog box on this activity only. One way to do it is by checking on every button's click(in onClick method) and then notifying the user about lost internet connection . But this may be very hectic for me as there are a lot of activities and a lot of buttons in every activity.
Is there a general and efficient way to find out if this activity is being left and a new activity is being started and after finding it, showing dialog box to the user rather than moving to next activity if internet is not available.What are the best practices for handling this issue. Please help me. I am stuck on this for last 2 days. Thanx in advance.
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.backIcon:
navigateMenuScreen(view);
break;
}
}
Here is how I check the internet connectivity(and it works fine):
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo!=null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
This is the navigation method:
public void navigateMenuScreen(View view){
try {
Intent loginIntent = new Intent(view.getContext(), MenuActivity.class);
startActivityForResult(loginIntent, 0);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
} catch(Exception ex) {
}
}
My question is do I have to check internet connectivity in every navigation method(there are other methods like the above one) or is there a better way?
Upvotes: 0
Views: 247
Reputation: 26766
On possible solution (assuming you're developing for Android 3.0+) is to use the AsyncTaskLoader
(docs). This would mean that when the user pushes a button on Activity1
, you launch a "background" task which retrieves your data. When it's successfully downloaded the data, it can then display Actiivty2
.
This would guarantee data by the time Activity2
is displayed but wouldn't guarantee that Activity2
is shown immediately (or at all, depending on how you write it)
I'm not sure this is quite what you're after but it's worth having a read.
Upvotes: 1
Reputation: 22527
Register
registerReceiver(mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Listen:
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
// do application-specific task(s) based on the current network state, such
// as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
}
};
And dont forget your manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Upvotes: 2