How to call onDestroy method in Activity

Published October 03, 2021

In this android example we will learn how to call onDestroy method in activity. onDestroy is the lifecycle method of activity, this activity lifecycle method will call on finish the activity. Let's call this onDestroy method in onCreate() method

Let's get started

Step 1: Create an Android application

Step 2: Add below code in Activity class

package com.rrtutors.kotlinprograms

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class ActivityDestroy : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_destroy)

       Thread.sleep(5000);
        onDestroy();

    }

    
}

 

Step 3: Run application

 

When we run the application, activity will start and after 5 seconds, we called onDestroy() method, so activity will automatically destroy.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1188 Views