Finish android activity from another with Kotlin

后端 未结 2 583
旧巷少年郎
旧巷少年郎 2021-01-21 14:13

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)

相关标签:
2条回答
  • 2021-01-21 14:59

    Simple code to finish a particular activity from another:

    class SplashActivity : AppCompatActivity(), NavigationListner {
    
      class MyClass{
        companion object{
            var activity: Activity? = null
        }
      }
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MyClass.activity = this@SplashActivity
      }
    
    
      override fun navigateFromScreen() {
        val intent = Intent(this,LoginActivity::class.java)
        startActivity(intent)
      }
    }
    

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

    0 讨论(0)
  • 2021-01-21 15:14

    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.

    0 讨论(0)
提交回复
热议问题