Destroy activity and reopen it by running everything again

0

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);
        }
    });
}

} enter the description of the image here

    
asked by Juan 16.10.2018 в 03:21
source

2 answers

1

You should improve the code so you do not need to restart the activity

But if you still want to restart it here it is like:

   Intent intent = getIntent(); 
   finish();        
   startActivity(intent);
    
answered by 16.10.2018 в 03:27
0

Hello with this code you make sure that everything is run again and clean the Activities stack you have open.

    Context mContext = this.getApplicationContext();
    Intent i = mContext.getPackageManager().getLaunchIntentForPackage( mContext.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(i);

I hope you serve. Greetings.

    
answered by 16.10.2018 в 19:13