hhh
hhh

Reputation: 23

Launch new activity in AsyncTask in Kotlin

I want to launch an Activity or refresh the current one from AsyncTask with Kotlin in the onPostExecute method but I'm not sure how to do it.

I tried to implement an interface but I got stuck.

interface

 public interface inter {
        void newActivity(Activity activity);
    }

main class

override fun onPostExecute(result: String?) {
        super.onPostExecute(result)
        ..code here
        inter {
        }
}

override fun newActivity(activity: Activity?) {
            val intent = Intent(this, Activity::class.java)
            startActivity(intent)
}

Upvotes: 1

Views: 250

Answers (1)

Muazzam A.
Muazzam A.

Reputation: 645

Simply on onPostExecute

private class yourTask(context:Context): AsyncTask<Void, Void, Void>() {

     var context: Context = context;

    override fun onPostExecute(result: Void?) {
        super.onPostExecute(result)
        val intent = Intent(context, ChangePasswordActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

}

Start task as:

yourTask(applicationContext).execute()

Upvotes: 1

Related Questions