user3465498
user3465498

Reputation:

multiple activities are starting when on click

I am calling one activity via intent when user clicks login button.When internet connection is poor user clicks the login button and activity is not started immediately so the user clicking the login button again. after some time two activities are opening.how can i resolve this. it is fine when internet connection is good.

thanks in advance

Upvotes: 1

Views: 48

Answers (3)

user3465498
user3465498

Reputation:

problem is solved with android:launchmode="singleTop" in manifest

Upvotes: 1

Aritra Roy
Aritra Roy

Reputation: 15615

Yes this should not happen.

You need to first check if there is a internet connection available (either via Mobile Internet and Wifi)

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

This method will check if there is an internet connection available either via Mobile Internet or Wifi. You can obviously tweak this to check if an high speed connection is available. But for a simple login, this is enough.

Now start an Activiy only when this method returns true otherwise don't.

Please ensure that you have this permission in the Manifest file,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Hope it helps you.

Upvotes: 0

Passiondroid
Passiondroid

Reputation: 1589

It is better to check internet connection and give proper message to user when internet is not available.

You can create below method in your class to do this -

public static boolean checkInternetConnection(Context context) {
 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // test for connection
    if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        return false;
    }
}

If it returns true then start new activity otherwise show inernet not available message.

Upvotes: 0

Related Questions