0

I am opening two activities at the same time. One is MainActivity and another one is CustomActivity with the button id returnbutton. I want to close the CustomActivity only by clicking the button and make MainActivity running. I use the code of this.finish() on button clicking but when I click it, my whole app finishes. Someone please help me to finish the CustomActivity only.This is my code

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent(application, CustomActivity::class.java)
        startActivity(intent)


                val button : Button = findViewById(R.id.returnButton)
        button.setOnClickListener {
            this.finish()

        }
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
Esoul
  • 41
  • 2
  • Add Logs form the logcat. – kelvin Sep 04 '20 at 04:20
  • i suggest you look at this -->https://stackoverflow.com/questions/11102337/can-you-have-two-activities-running-at-the-same-time – Wini Sep 04 '20 at 05:48
  • Yeah that's not how Activities work, the Button and Finish code needs to be in CustomActivity, you are currently launching CustomActivity and immediately finishing the root Activity, killing the app. – patrick.elmquist Sep 04 '20 at 09:10

1 Answers1

1

You have button in your MainActivity so when you are using this it is pointing to MainActivity and it is closing that activity, I would suggest you to add this Button and its onClick to CustomActivity.

kelvin
  • 1,480
  • 1
  • 14
  • 28