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?