Saturday, March 19, 2016

android - How to restart an Activity

MainActivity.java

package com.cfsuman.androidtutorials;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button buttonRestart = findViewById(R.id.buttonRestart);
        Button buttonRecreate = findViewById(R.id.buttonRecreate);
        TextView textView = findViewById(R.id.textView);


        // Set the TextView text with a random generated number
        textView.setText("Random number : "
                + new Random().nextInt(100));


        // Set a click listener for restart Button widget
        buttonRestart.setOnClickListener(view -> {
            Intent intent = getIntent();
            // Finish the activity
            finish();

            // Restart the activity
            startActivity(intent);
        });


        // Set a click listener for recreate Button widget
        buttonRecreate.setOnClickListener(view -> {
            // Recreate the activity
            recreate();
        });
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/buttonRestart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Restart Activity"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonRecreate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="Recreate Activity"
        app:layout_constraintBottom_toBottomOf="@+id/buttonRestart"
        app:layout_constraintStart_toEndOf="@+id/buttonRestart" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:textStyle="italic"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonRestart"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>