How to Restart Activity in Android?
Last Updated :
06 Feb, 2022
An Activity in Android is that element of the application that provides a platform for drawing UI elements. Activity, in general, occupies the entire screen but may not always fill the entire screen. MainActivity is the first activity that is created when a project is created. However, activities can be created later and set as the first activity via configuring the manifest file. In this article, we will show you how you could restart activity in Android. The idea of the project is to restart the activity and to observe it, by generating a random number. So whenever the activity restarts a random number is displayed indicating the app has restarted. Now follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView to display a random number, and a Button to execute the process.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/text_view_1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_marginTop = "50sp"
android:gravity = "center" />
< Button
android:id = "@+id/button_1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:text = "restart" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.geeksforgeeks.restartactivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mTextView = findViewById<TextView>(R.id.text_view_1)
val mButton = findViewById<Button>(R.id.button_1)
val mRandom = Random()
mTextView.text = mRandom.nextInt( 50 ).toString()
mButton.setOnClickListener {
val mIntent = intent
finish()
startActivity(mIntent)
}
}
}
|
Output:
You can see that when the button is clicked, the app restarts and a random number is displayed at every start.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...