片段:回调时调用preSS后退按钮和放大器;自定义 [英] Fragment: which callback invoked when press back button & customize it
问题描述
我有一个片段:
公共类MyFragment扩展片段{
...
@覆盖
公共查看onCreateView(...){...}
...
}
我实例吧:
MyFragment myFragment =新MyFragment();
我用上面的片段,以取代目前的片段:
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//替换片段
fragmentTransaction.replace(R.id.fragment_placeholder,myFragment,MyTag的);
//注:我没有添加到回栈
现在, myFragment
正显示在屏幕上。注:我没加 myFragment
到返回堆栈
我的两个问题:
1 如果现在,我preSS手机返回按钮,该片段的生命周期回调会被调用?
2 如何自定义返回按钮点击监听器 MyFragment
类? (请不要建议我做 myFragment.getView()。setOnclickListener
,但这样做在 MyFragment
类)
问题1:见的 http://developer.android.com/reference/android/app/Fragment.html#Lifecycle :
作为一个片段不再被使用时,它通过一个反向串联回调:
的onPause() - 片段不再与用户交互可能是因为它的活性被暂停或一个片段操作 修改它在活动
的onStop() - 片段不再对用户可见或者是因为它的活性被停止或片段操作被修改它 在活动
onDestroyView() - 允许碎片清理其查看相关资源
的onDestroy() - 所谓做片段的状态最终清理
onDetach() - 。叫之前立即不再与它的活动相关联的片段
问题2:如果你要知道,这是后退按钮的的专门 被触发回调,您可以捕获后退按钮preSS在片段的活动并使用自己的方法来处理它:
公共类MyActivity扩展活动
{
// ...
//定义在活动类,所以覆盖
@覆盖
公共无效onBack pressed()
{
super.onBack pressed();
myFragment.onBack pressed();
}
}
公共类MyFragment扩展片段
{
//你的创造方法
公共无效onBack pressed()
{
//处理你不要总想在正常的生命周期做任何清理
}
}
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
}
}
这篇关于片段:回调时调用preSS后退按钮和放大器;自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!