0

I am trying to delete my app data programmatically using Kotlin as my programing language but did not find any solution.

I have a WebView and it is reloading my URL after a specified time period. After the specified time is over I want to delete my app data and reload my WebView to delete any stored cache or other files stored from the previous website visit.

What I tried so far

package com.technosoftkpk.y_view
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import kotlin.concurrent.thread
import kotlin.random.Random


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val myWebView: WebView = findViewById(R.id.webview)
        myWebView.setWebViewClient(WebViewClient())
        myWebView.loadUrl("https://www.technosoft.eu")
        val webSettings = myWebView.settings
        webSettings.javaScriptEnabled = true

        //Random Timer To reload the page
        thread {
            while (true) {
                Thread.sleep(5000)
                // How to add function below to delete app data programmatically after the above time period.
                // function bla bla bla here


                Thread.sleep(5000)
                runOnUiThread { myWebView.reload() }

            }

        }

    }

}

I hope you will understand what I am trying to do. Thanks in advance

Elizabeth Harper
  • 131
  • 1
  • 12
  • Detailed Answered by @TWiStErRob [Answere](https://stackoverflow.com/a/29197259/3546909) – drjansari Mar 27 '21 at 08:15
  • Does this answer your question? [Clear Application's Data Programmatically](https://stackoverflow.com/questions/6134103/clear-applications-data-programmatically) – a_local_nobody Mar 27 '21 at 10:53

1 Answers1

1

I want to answer my question here because I found the perfect solution to my problem.

My function for clearing app data.

private fun deleteAppData() {
        try {
            // clearing app data
            val packageName = applicationContext.packageName
            val runtime = Runtime.getRuntime()
            runtime.exec("pm clear $packageName")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

My call to this function after some time period via thread

thread {
       while (true) {
       Thread.sleep(5000)
       runOnUiThread { deleteAppData() }
       }
     }
Elizabeth Harper
  • 131
  • 1
  • 12