Home > Software engineering >  Navigation from Activity to previous location (previous tab(fragment) and other Activity)
Navigation from Activity to previous location (previous tab(fragment) and other Activity)

Time:08-24

I am using Kotlin. I faced the problem of the navigation from Activity to previous location. The ArtikelContent Activity can be open by 3 different location which are: 1.Home menu 2.Artikel home - at the "Semua" tab (Fragment Semua) 3. Artikel home - at the "KAT 1" tab (Fragment KAT 1). Im using imageview as a back button on Artikel Content. Im faced the problem to navigate to the previous location before i open the ArtikelContent Activity by using imageview as a button.

This is the code i use on the ArtikelContent.kt Activity for back navigation:

val btnback : ImageView = findViewById(R.id.buttonback)

    btnback.setOnClickListener {
        val intent = Intent (this, ArtikelHome::class.java)
        startActivity(intent)
    }

CodePudding user response:

You are creating a new instance of ArtikelHome activity in this btnBack click listener because of the new Intent. If you want to navigate back to the ArtikelHome activity then use finish() like this:

btnback.setOnClickListener {
    finish()
}

It will destroy the ArtikelContent activity and you will be navigated to the previous activity in stack which is ArtikelHome activity.

  • Related