哪些呢onBack preSS呼叫一个片段 [英] what does onBackPress call in a Fragment
问题描述
在一个片段,我有这样的:
In a Fragment i have this:
onPause(){
super.onPause();
if(flag){
getActivity.finish();
}else{
}
}
onResume(){
flag = true;
super.onResume();
}
和有一个按钮,在点击我这个标志设置为false:
and there is a Button, on click i set this flag to false:
Button.onClick{
flag = false;
}
的理念是:
在按钮点击不完成此活动。但是,当设备的后退按钮为pressed完成()。
when the button is clicked don't finish this Activity. But when device BackButton is pressed finish().
但这种逻辑是行不通的。其原因是,当I $设备的对$ PSS的后退按钮 的onPause 不会被调用。
But this logic isn't working. The reason is, when i press BackButton of the device, onPause is not being called.
这是在Activty第四片段。所以,当我preSS后退按钮,我可以看到第三个片段。我不想要。
This is the 4th Fragment in the Activty. So when i press BackButton i can see the 3rd Fragment. Which i don't want to.
我使用这个API 10设备上,但我的应用程序使用支持库V4。
感谢你。
推荐答案
如果您要关闭的背面preSS的活动,然后重写 onBack pressed
在活动如下里面:
If you want to close the activity on the back press, then Override the onBackPressed
inside the activity as following:
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("yourfragment");
if (fragment instanceOf YourFragmet) {
finish();
return;
}
super.onBackPressed();
}
如果您要关闭按钮点击活动,那么您可以致电
If you want to close the activity on the button click, then you can call
Button.onClick{
getActivity().onBackPressed();
}
或
Button.onClick{
getActivity().finish();
}
如果当你片段之间的转换,你可以调用addToBackStack()作为FragmentTransaction的一部分,那么后面的按钮将带你到片段上回堆栈的顶部
If When you are transitioning between Fragments, you call addToBackStack() as part of your FragmentTransaction, then the back button will take you to the fragment on the top of the back stack
这篇关于哪些呢onBack preSS呼叫一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!