1

What is the best way to Redirect my user to another activity programmatically? Most of the codes I researched are using deprecated resources and some do not work again, they are also only work on button click?

How can I make it that when users arrive to my splash screen, if the system detects that they are not connected to the internet it automatically redirects to an activity specified by me.

NB: This is not a duplicate resource I have checked out other resource on Stackoverflow many of which only talk about how to check connection, what about those who which to make a redirect when the system checks that the internet is not connected in API 30?

I tried this which is not automatic, it only works on button click

  signupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo wifi = cm
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo datac = cm
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if ((wifi != null & datac != null)
                    && (wifi.isConnected() | datac.isConnected())) {
                Intent i = new Intent(StudentAppActivity.this, RegisterActivity.class);
                startActivity(i);
            }else{
                //no connection
                Intent i = new Intent(StudentAppActivity.this, Splash.class);
                startActivity(i);
            }
        }
    });

The problem I have with this code is that, assuming users launch my app for the first time without internet on, when they click on that button I specified here, the APP crashes, but when they turn on their data and open the app, then turn it off and click on the button, the code redirects well, but that will create a bad user experience assuming the user gets the app from a friend opens it without data and click on the button then the APP crash

What is the accurate way to achieve this in API 30?

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
  • Use Networkcallbacks to keep track of the current state of Connection and keep the current connectivity in a livedata variable – Dominik Wuttke May 22 '21 at 11:21
  • I am not sure I understand this. – Bluechip Servers May 22 '21 at 11:34
  • Adding to what @DominikWuttke mentioned. You can register a n/w change listener as mentioned in [ans](https://stackoverflow.com/questions/27144026/how-can-i-receive-a-notification-when-the-device-loses-network-connectivity-in-a). Based on callback you receive, you can perform action to show offline activity. – Ayush May 22 '21 at 11:44

1 Answers1

1

You can use ConnectivityManager to check if the user has access to internet.

Code example:

boolean isConnectedToInternet() {

ConnectivityManager cm =
        (ConnectivityManager)SplashScreenActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();


return isConnected;

}

And then you can check the connection from onCreate() to send the user to another screen:

if (isConnectedToInternet()) {
   // user is connected to the internet
}else {
   // user is not connected to the internet
}
GSepetadelis
  • 272
  • 9
  • 24
  • Thank you, this looks well explained. Can I get a link to any resource where I can get a full working code for the ConnectivityManager? – Bluechip Servers May 22 '21 at 12:46
  • @BluechipServers You can just copy-paste the part of code witch i send you in my reply. You can also check the official Android `ConnectivityManager` documentation here: https://developer.android.com/reference/android/net/ConnectivityManager – GSepetadelis May 22 '21 at 13:05
  • Ok, where I am not too clear is, where inside my SplashScreenActivity will I put the ConnectivityManager code? Should I paste my SplashScreen code, so I do not make mistake, I am only learning from you and basically a newbie in coding. – Bluechip Servers May 22 '21 at 13:14
  • @BluechipServers Yes, paste the `isConnectedToInternet()` function outside the `onCreate()` and inside the `onCreate()` paste the if\else statement :-) – GSepetadelis May 22 '21 at 14:27
  • @GSpetadelis thanks your answer helped me. – Bluechip Servers May 22 '21 at 15:34