如何在片段中实现onBackPared()?我们有没有办法onBackPressed()在Android片段中,类似于我们在Android活动中实现的方式?因为片段生命周期没有onBackPressed()..有什么其他方法可以替代骑吗?onBackPressed()在Android 3.0片段中?
3 回答
ABOUTYOU
TA贡献1812条经验 获得超5个赞
onBackPressed
FragmentTransaction
addToBackStack
@Overridepublic void onBackPressed() { int count = getSupportFragmentManager().getBackStackEntryCount(); if (count == 0) { super.onBackPressed(); //additional code } else { getSupportFragmentManager().popBackStack(); }}
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
Java解决方案
public interface IOnBackPressed { /** * If you return true the back press will not be taken into account, otherwise the activity will act naturally * @return true if your processing has priority if not false */ boolean onBackPressed();}
public class MyActivity extends Activity { @Override public void onBackPressed() { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container); if (!(fragment instanceof IOnBackPressed) || !((IOnBackPressed) fragment).onBackPressed()) { super.onBackPressed(); } } ...}
public class MyFragment extends Fragment implements IOnBackPressed{ @Override public boolean onBackPressed() { if (myCondition) { //action not popBackStack return true; } else { return false; } }}
Kotlin解
1-创建接口
interface IOnBackPressed { fun onBackPressed(): Boolean}
2-准备你的活动
class MyActivity : AppCompatActivity() { override fun onBackPressed() { val fragment = this.supportFragmentManager.findFragmentById(R.id.main_container) (fragment as? IOnBackPressed)?.onBackPressed()?.not()?.let { super.onBackPressed() } }}
3-在目标片段中实现
class MyFragment : Fragment(), IOnBackPressed { override fun onBackPressed(): Boolean { return if (myCondition) { //action not popBackStack true } else { false } }}
www说
TA贡献1775条经验 获得超8个赞
BaseActivity
创建类接口 OnBackPressedListener
public interface OnBackPressedListener { public void doBack();}
创建实现以下技能的类: OnBackPressedListener
public class BaseBackPressedListener implements OnBackPressedListener { private final FragmentActivity activity; public BaseBackPressedListener(FragmentActivity activity) { this.activity = activity; } @Override public void doBack() { activity.getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); }}
从现在开始,我们将编写我们的代码。 BaseActivity
及其碎片 在类的顶部创建私有侦听器 BaseActivity
protected OnBackPressedListener onBackPressedListener;
创建方法来设置侦听器 BaseActivity
public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) { this.onBackPressedListener = onBackPressedListener;}
在覆盖 onBackPressed
实现这样的东西 @Overridepublic void onBackPressed() { if (onBackPressedListener != null) onBackPressedListener.doBack(); else super.onBackPressed();
在你的碎片里 onCreateView
您应该添加我们的侦听器 @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { activity = getActivity(); ((BaseActivity)activity).setOnBackPressedListener(new BaseBackPressedListener(activity)); View view = ... ;//stuff with view return view;}
- 3 回答
- 0 关注
- 483 浏览
添加回答
举报
0/150
提交
取消