Saturday, March 19, 2016

android - How to restart an Activity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#e4e3de"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        android:padding="25dp"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Restart This Activity"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Recreate This Activity"
        android:layout_toRightOf="@id/btn"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.Intent;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButton;
    private Button mButtonSecond;

    private Random mRandom = new Random();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);
        mButtonSecond = (Button) findViewById(R.id.btn2);

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

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    getIntent()
                        Return the intent that started this activity.
                */
                // Get the current activity
                Intent intent = getIntent();

                /*
                    public void finish ()
                        Call this when your activity is done and should be closed. The ActivityResult
                        is propagated back to whoever launched you via onActivityResult().

                */
                // Finish the activity
                finish();

                /*
                    public void startActivity (Intent intent)
                        Same as startActivity(Intent, Bundle) with no options specified.

                    Parameters
                        intent : Intent: The intent to start.
                    Throws
                        android.content.ActivityNotFoundException
                */
                // Restart the activity
                startActivity(intent);
            }
        });

        // Set a click listener for the second button widget
        mButtonSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    public void recreate ()
                        Cause this Activity to be recreated with a new instance. This results in
                        essentially the same flow as when the Activity is created due to a
                        configuration change -- the current instance will go through its lifecycle
                        to onDestroy() and a new instance then created after it.
                */
                // Recreate the activity
                recreate();
            }
        });
    }
}