Skip to content Skip to sidebar Skip to footer

Finish Android Activity From Another With Kotlin

I'm trying to finish an activity from another (android) with kotlin. I know the wat to do it with java is with the following code (https://stackoverflow.com/a/10379275/7280257) at

Solution 1:

Simple code to finish a particular activity from another:

classSplashActivity : AppCompatActivity(), NavigationListner {

  classMyClass{
    companionobject{
        var activity: Activity? = null
    }
  }

  overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    MyClass.activity = this@SplashActivity
  }


  overridefunnavigateFromScreen() {
    val intent = Intent(this,LoginActivity::class.java)
    startActivity(intent)
  }
}

Now call SplashActivity.MyClass.activity?.finish() from another activity to finish above activity.

Solution 2:

The error Expecting member declaration is there because you wrote a statement (the function call) inside a class. In that scope, declarations (functions, inner classes) are expected.

You have to place your statements inside functions (and then call those from somewhere) in order for them to be executed.

Post a Comment for "Finish Android Activity From Another With Kotlin"