How to stop music from playing when user switch to other page in Android Studio Kotlin?


I have created a meditation application that users can select the class they want from the recycler view within the fragment, then it will show the detailed content and steps in a new activity. In the new activity, I have implemented a audio playing functions where user can stream the audio file from the firebase. The problem now is whenever I switch back to the recycler view within the fragment, the audio file still plays in the background. How do I stop the audio file from playing whenever I switch to another page within the app or switch to another app ?

This is the code for the recycler view within the fragment

class ClassFragment : Fragment() {

private lateinit var recyclerView: RecyclerView
private lateinit var classArrayList: ArrayList<Classes>
private lateinit var tempArraylist: ArrayList<Classes>
private lateinit var classAdapter: ClassAdapter
private lateinit var db: FirebaseFirestore
var mediaPlayer: MediaPlayer? = null

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view: View = inflater.inflate(R.layout.fragment_class, container, false)
    recyclerView = view.findViewById(
    recyclerView.layoutManager = LinearLayoutManager(context)

    classArrayList = arrayListOf()
    tempArraylist = arrayListOf()
    classAdapter = ClassAdapter(requireContext(), classArrayList)

    recyclerView.adapter = classAdapter

    return view

private fun eventChangeListener() {
    db = FirebaseFirestore.getInstance()
    db.collection("class").addSnapshotListener(object : EventListener<QuerySnapshot> {
        override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
            if (error != null) {
                Log.e("Firestore error", error.message.toString())
            for (dc: DocumentChange in value?.documentChanges!!) {
                if (dc.type == DocumentChange.Type.ADDED) {



This is the code for the new activity page where it will show content when the user selects from recyclerview

class DetailActivity : AppCompatActivity() {

private lateinit var imageViewClass: ImageView
private lateinit var textViewClassName: TextView
private lateinit var textViewClassDes: TextView
private lateinit var textViewClassContent: TextView
private var stop:Boolean = false
var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
    val btnPlay: Button = findViewById(
    val btnStop: Button = findViewById(

    imageViewClass = findViewById(
    textViewClassName = findViewById(
    textViewClassDes = findViewById(
    textViewClassContent = findViewById(

    val classImage = intent.getStringExtra("class_image")
    val className = intent.getStringExtra("class_name")
    val classDes = intent.getStringExtra("class_des")
    val classContent = intent.getStringExtra("class_content")
    val classAudio = intent.getStringExtra("class_audio")

    textViewClassName.text = className
    textViewClassDes.text = classDes
    textViewClassDes.text = classDes!!.replace("\\n", "\n")
    textViewClassContent.text = classContent
    textViewClassContent.text = classContent!!.replace("\\n", "\n")

    btnPlay.isEnabled = true
    btnStop.isEnabled = false

    btnPlay.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this@DetailActivity, Uri.parse(classAudio))
                mediaPlayer!!.isLooping = true
                btnStop.isEnabled = true
                btnPlay.isEnabled = false
                Toast.makeText(applicationContext,"Audio Starts",Toast.LENGTH_SHORT).show()
            } else mediaPlayer!!.start()



    mediaPlayer?.setOnCompletionListener {
        btnPlay.isEnabled = true
        btnStop.isEnabled = false

    btnStop.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
                stop = false
                mediaPlayer = null
                btnPlay.isEnabled = true
                btnStop.isEnabled = false
                Toast.makeText(applicationContext,"Audio Stops",Toast.LENGTH_SHORT).show()


This is a classic case of onDestroy of the Activity being called. I’ll keep it short but there is a lifecycle through which an Activity undergoes and when you seem to come back from the SecondActivity to the first Activity, the onDestroy of your second activity is called to perform the destruction function

So the solution, override the onDestroy method in your DetailActivity class and do something like this

override fun onDestroy(){
 if(mediaPlayer != null){
   mediaPlayer = null

You can read in detail about Activity Lifecyle

Answered By – gtxtreme

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published