今天又要回到專案的部分,完成了頁面間的切換,接下來要來做返回的功能,我們要使用手機內建的返回功能鍵來做頁面的返回,這個按鍵在觸發時會運行onBackPressed()的方法。
首先來做幾個抽象類別,一樣先創建兩個新檔案。
BaseFragment:
abstract class BaseFragment : Fragment() {
private var mActivity: BaseActivity? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is BaseActivity) {
this.mActivity = context
}
}
override fun onDetach() {
mActivity = null
super.onDetach()
}
interface Callback {
fun onFragmentDetached(arg: Bundle?, vararg tag: String)
}
}
BaseActivity:
abstract class BaseActivity : AppCompatActivity(), BaseFragment.Callback {
override fun onFragmentDetached(arg: Bundle?, vararg tag: String) {
}
}
接下來讓MainActivity來繼承上面的BaseActivity,我們需要在onFragmentDetached中做切換片段的設定。
MainActivity:
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
CommonUtils.TransFragment(this, R.id.main_container, MainFragment(), MainFragment().TAG, null)
}
override fun onFragmentDetached(arg: Bundle?, vararg tag: String) {
super.onFragmentDetached(arg, *tag)
val fragmentManager = supportFragmentManager
val fragment: Fragment = fragmentManager.findFragmentByTag(tag[0])!!
fragmentManager
.beginTransaction()
.disallowAddToBackStack()
.remove(fragment)
.commitNow()
}
//返回頁面
override fun onBackPressed() {
val fragmentManager = supportFragmentManager
when {
null != fragmentManager.findFragmentByTag(NoteFragment().TAG) -> onFragmentDetached(null, NoteFragment().TAG)
null != fragmentManager.findFragmentByTag(SelectFragment().TAG) -> onFragmentDetached(null, SelectFragment().TAG)
else -> super.onBackPressed()
}
}
}
如次便能夠統一在MainActivity下來完成返回的功能,未來有新增的fragment依照上面的onBackPressed內容做新增便能夠完成切換的工作,下面統整其他前面所做的內容。
NoteFragment:
class NoteFragment : Fragment() {
companion object val TAG = NoteFragment::class.java.simpleName
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_note,container,false)
}
}
SelectFragment:
class SelectFragment : Fragment() {
companion object
val TAG = SelectFragment::class.java.simpleName
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_select, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
CommonUtils:
object CommonUtils {
fun TransFragment(context: Context, id: Int, fragment: Fragment?, tag: String, bundle: Bundle?) {
if (fragment == null) return
fragment.arguments = bundle
val appCompatActivity = context as AppCompatActivity
appCompatActivity.supportFragmentManager
.beginTransaction()
.disallowAddToBackStack()
.add(id, fragment, tag)
.commit()
}
}
MainFragment:
class MainFragment : Fragment() {
companion object
val TAG = MainFragment::class.java.simpleName
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
}
private fun initViews() {
select.setOnClickListener {
CommonUtils.TransFragment(context!!, R.id.main_container, SelectFragment(), SelectFragment().TAG, null)
}
note.setOnClickListener {
CommonUtils.TransFragment(context!!, R.id.main_container, NoteFragment(), NoteFragment().TAG, null)
}
}
}
最後的package內容