代码如下:

 class XXXFrgmt : Fragment() {

     override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listenerBackKey()
} fun listenerBackKey(){
view!!.isFocusableInTouchMode = true
view!!.requestFocus()
view!!.setOnKeyListener(View.OnKeyListener { view, i, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_UP && i == KeyEvent.KEYCODE_BACK) {
if (::callback.isInitialized){
callback.invoke(results)
}
}
false
})
} override fun onDestroyView() {
super.onDestroyView()
} override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
} override fun onPause() {
super.onPause()
} override fun onStop() {
super.onStop()
} override fun onResume() {
super.onResume()
} }

要求:

  这个Fragment要是add或者replace 主布局R.id.main的fragment,子fragment不可以 ,如,viewpager里的fragment则不可以.

  view!! 是

      /**
* Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
* if provided.
*
* @return The fragment's root view, or null if it has no layout.
*/
@Nullable
public View getView() {
return mView;
}
05-17 21:28