I have the following code that validates if there is a connection or not, I have different elements that are shown depending on the status, if there is no connection I can see a button that says Try again, I need to click on that button, the app It will restart and run again all over again. Try with finish () and others that do it but the ringProgress does not run again and the circle remains doing nothing (retry_conexion.setOnClickListener) I need to destroy everything and start again as if I had opened it recently.
public class SplashActivity extends AppCompatActivity {
static RingProgressBar ringProgressBar;
static int progress = 0;
ImageView no_connect;
Button reintentar_conexion;
private static class MyHandler extends Handler {
public void handleMessage(Message msg) {
if (msg.what == 0) {
if (progress < 100) {
progress++;
ringProgressBar.setProgress(progress);
}
}
}
}
private final MyHandler myHandler = new MyHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ringProgressBar = findViewById(R.id.progress_bar_1);
no_connect = findViewById(R.id.no_connect);
reintentar_conexion = findViewById(R.id.reintentar_conexion);
final Intent i = new Intent(this, MainActivity.class);
final Intent sa = new Intent(this, SplashActivity.class);
no_connect.setVisibility(View.GONE);
reintentar_conexion.setVisibility(View.GONE);
ringProgressBar.setVisibility(View.VISIBLE);
ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
assert con != null;
final NetworkInfo networkInfo = Objects.requireNonNull(con).getActiveNetworkInfo();
ringProgressBar.setOnProgressListener(new RingProgressBar.OnProgressListener() {
@Override
public void progressToComplete() {
if(networkInfo !=null && networkInfo.isConnected()){
startActivity(i);
}
else{
Toast.makeText(SplashActivity.this, "Verifica tu conexion", Toast.LENGTH_SHORT).show();
no_connect.setVisibility(View.VISIBLE);
reintentar_conexion.setVisibility(View.VISIBLE);
ringProgressBar.setVisibility(View.GONE);
}
}
});
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<100;i++){
try{
Thread.sleep(40);
myHandler.sendEmptyMessage(0);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}).start();
reintentar_conexion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
startActivity(sa);
}
});
}