0

I am trying to go back to another fragment, but the android studio keep saying that I cant override onBackPressed():

package com.educator.dualsignal

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class DraftFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.draft_layout,null)
    }

    override fun onBackPressed() {
        val fm = fragmentManager
        fm!!.popBackStackImmediate()
    }
}

2 Answers 2

2

onBackPressed() is a method in the Activity class. The Fragment class has no such method.

You need to implement your backstack logic in the parent Activity.

2

onBackPressed() is for activity, if you want to back pre fragment by clicking on back button you can add your fragments to a stack and when user tap on back button fragments pop. https://medium.com/@bherbst/managing-the-fragment-back-stack-373e87e4ff62

Not the answer you're looking for? Browse other questions tagged or ask your own question.