Abhilash Murali Abhilash Murali - 22 days ago 9
Android Question

App stuck at Splash screen even after enabling internet connection

I set my splash activity to check for active internet connection. If there is no internet connection, I've set an alert dialog to pop along with a button to open internet settings. Now the issue i face is that even after enabling internet connection and going bac, it is stuck in splash screen. App doesn't open the new activity.

SplashActivity.java

public class SplashActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

boolean mobileNwInfo;
ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
try { mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); }
catch (NullPointerException e) { mobileNwInfo = false; }
if (!mobileNwInfo) {
new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
.setTitleText("Info ")
.setCustomImage(R.mipmap.alert)
.setContentText("No internet connection!")
.setConfirmText("OPEN SETTINGS")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.dismissWithAnimation();
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
})
.show();
}
else
{
Splash();
}


}

public void Splash()
{
int SPLASH_TIME_OUT = 5000;
new Handler().postDelayed(new Runnable() {

@Override
public void run() {

Intent i = new Intent(SplashActivity.this, Main2Activity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}

}

Answer

You have to check internet condition in onResume(). Try below code:

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!checkInternet()) {
            new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                    .setTitleText("Info ")
                    .setCustomImage(R.mipmap.alert)
                    .setContentText("No internet connection!")
                    .setConfirmText("OPEN SETTINGS")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {
                            sDialog.dismissWithAnimation();
                            startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                        }
                    })
                    .show();
        } else {
            Splash();
        }
    }

    public boolean checkInternet() {
        boolean mobileNwInfo;
        ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        try {
            mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected();
        } catch (NullPointerException e) {
            mobileNwInfo = false;
        }
        return mobileNwInfo;
    }

    public void Splash() {
        int SPLASH_TIME_OUT = 5000;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashActivity.this, Main2Activity.class);
                startActivity(i);
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
}