Fragment: which callback invoked when press back button amp; customize it(片段:按下返回按钮时调用哪个回调 amp;定制它)
问题描述
我有一个片段:
公共类 MyFragment 扩展 Fragment{...@覆盖公共视图 onCreateView(...){...}...}
我实例化它:
MyFragment myFragment = new MyFragment();
我用上面的片段来替换当前的片段:
FragmentManager fragmentManager = activity.getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//替换片段fragmentTransaction.replace(R.id.fragment_placeholder, myFragment, "myTag");//注意:我没有添加到后台堆栈
现在,myFragment
显示在屏幕上.注意:我没有将 myFragment
添加到 back stack.
我的两个问题:
1.如果现在我按下手机返回按钮,会调用哪个fragment的生命周期回调??
2.如何自定义MyFragment
类中的后退按钮点击监听?(请不要建议我做 myFragment.getView().setOnclickListener
,而是在 MyFragment
类中做)
问题一:见http://developer.android.com/reference/android/app/Fragment.html#Lifecycle:
<块引用><块引用>当一个片段不再被使用时,它会经历一系列反向的回调:
onPause() - 片段不再与用户交互,因为它的活动被暂停或片段操作被暂停在活动中修改它.
onStop() - 片段不再对用户可见,因为它的活动正在停止或片段操作正在修改它在活动中.
onDestroyView() - 允许片段清理与其视图关联的资源.
onDestroy() - 调用以对片段的状态进行最终清理.
onDetach() - 在片段不再与其活动关联之前立即调用."
问题 2:如果您必须知道触发回调的是后退按钮具体,您可以在 Fragment 的 Activity 中捕获后退按钮按下并使用你自己的处理方法:
公共类 MyActivity 扩展 Activity{//...//定义在Activity类中,所以重写@覆盖公共无效 onBackPressed(){super.onBackPressed();myFragment.onBackPressed();}}公共类 MyFragment 扩展了 Fragment{//你创建的方法公共无效 onBackPressed(){//处理您在正常生命周期中并不总是希望完成的任何清理工作}}
I have a fragment:
public class MyFragment extends Fragment{
...
@Override
public View onCreateView(...){...}
...
}
I instantiate it:
MyFragment myFragment = new MyFragment();
I use the above fragment to replace the current fragment:
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// replace fragment
fragmentTransaction.replace(R.id.fragment_placeholder, myFragment, "myTag");
// NOTE: I did not add to back stack
Now, myFragment
is showing on the screen. NOTE: I did not add myFragment
to back stack.
My two questions:
1. If now, I press mobile phone back button, which fragment's life cycle callback will be invoked??
2. How can I customize the back button click listener in MyFragment
class? (please do not suggest me to do myFragment.getView().setOnclickListener
, but do it in MyFragment
class)
Question 1: See http://developer.android.com/reference/android/app/Fragment.html#Lifecycle:
"As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause() - fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onStop() - fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
onDestroyView() - allows the fragment to clean up resources associated with its View.
onDestroy() - called to do final cleanup of the fragment's state.
onDetach() - called immediately prior to the fragment no longer being associated with its activity."
Question 2: If you must know that it was the back button specifically that is triggering the callbacks, You can capture the back button press in your Fragment's Activity and use your own method to handle it:
public class MyActivity extends Activity
{
//...
//Defined in Activity class, so override
@Override
public void onBackPressed()
{
super.onBackPressed();
myFragment.onBackPressed();
}
}
public class MyFragment extends Fragment
{
//Your created method
public void onBackPressed()
{
//Handle any cleanup you don't always want done in the normal lifecycle
}
}
这篇关于片段:按下返回按钮时调用哪个回调 &定制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:片段:按下返回按钮时调用哪个回调 &定制它
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01