views:

31

answers:

1

I am checking to see if my app has a network connection:

public boolean isOnline(){
        ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

        if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
            ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {

            return true;

        }
        else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
            ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
            return false;

        } else {return false;}
    }

Whenever I rotate my screen between landscape and portrait this method returns false. It makes me wonder if Network connections are getting killed during the rotation?

A: 

No... your network connections don't get killed during rotation. Probably, what is going on is that you are not saving the boolean value of the connection state, so when the handset is rotated that value gets to its default (false). Check this thread to know how to save the state: How do I save an Android application's state?

Cristian
do I need to save state? this method gets called onStart() so whenever the phone is rotated, it will get called?
Sheehan Alam
What do you mean? Do you have something like `var = isOnline()` on the `onStart` method?
Cristian
if(isOnline()){...}
Sheehan Alam
In that case, there's another weird problem. Could you please paste the whole code here (or in pastie.org if it's too long)?
Cristian