问题描述
我有一个片段:
公共类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:见的:
问题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:
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后退按钮和放大器;自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!