Kotlin 片段:activity returns 在 onActivityCreated 之外为空

Kotlin fragment : activity returns null outside onActivityCreated

我正在尝试在片段中使用 startActionMode

startActionModeonActivityCreated 方法中工作正常。但是在 onActivityCreated 方法之外,activity returns null 因此 activity?.startActionMode(mActionModeCallback) 不起作用。

package com.akshat.music
import android.app.Activity
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_songs.*
import java.io.File


class SongsFragment : Fragment(), SongListAdapter.OnItemClickListener{

private val allSongs = ArrayList<Songs>()
var activityRef: FragmentActivity? = null


override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    activityRef = activity;
    if (activityRef == null)
        Log.d("MIKE", " $activityRef is empty");
    else
        Log.d("MIKE", " $activityRef is not empty");
    recyclerView.apply {
        layoutManager = LinearLayoutManager(activity)
        adapter = SongListAdapter(allSongs, SongsFragment().apply { this@SongsFragment })
    }
    activity?.startActionMode(mActionModeCallback) // works

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    getAudioDirectories()
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater!!.inflate(R.layout.fragment_songs, container, false);
}

override fun onItemClicked(song: Songs, position: Int) {
    if (activityRef == null)
        Log.d("MIKE", " $activityRef is empty");
    else
        Log.d("MIKE", " $activityRef is not empty");
    activity?.startActionMode(mActionModeCallback)  // doesn't work
    activityRef?.startActionMode(mActionModeCallback) // activityRef is null
}

private val mActionModeCallback: ActionMode.Callback =
    object : ActionMode.Callback {
        override fun onCreateActionMode(
            actionMode: ActionMode,
            menu: Menu
        ): Boolean {
            val menuInflater = actionMode.menuInflater
            menuInflater.inflate(R.menu.contextual_action_bar, menu)
            return true
        }

        override fun onPrepareActionMode(
            actionMode: ActionMode,
            menu: Menu
        ): Boolean {
            return false
        }

        override fun onActionItemClicked(
            actionMode: ActionMode,
            menuItem: MenuItem
        ): Boolean {
            return false
        }

        override fun onDestroyActionMode(actionMode: ActionMode) {
            var actionMode: ActionMode? = actionMode
            actionMode = null
        }
    }

private fun getAudioDirectories() {
    /////////////
    }
}
}

如何在 onActivityCreated 方法之外获取对 activity 的引用? 谢谢

您可以将上下文转换为 Activity 并从那里调用它,例如

override fun onItemClicked(song: Songs, position: Int) {
    ...
    (requireContext() as Activity).startActionMode(mActionModeCallback)
}

您应该能够从片段内的任何位置获取对 activity 的引用。 在某些情况下 activity 可以为空,例如在 onDetach()OnCreateView() 中,因此 activity 并不总是需要一个值。

尝试像 Stachu 提到的那样使用 requireContext():

    (requireContext() as Activity).startActionMode(mActionModeCallback)