控制片段内部的后退按钮 [英] Controlling back button inside Fragment

查看:1057
本文介绍了控制片段内部的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编程经验,但在Android中经验不足.我认为,在任何平台上,我们都不允许用户离开屏幕而没有数据已保存.保存之前,我们还必须确保数据正确.

在Android中,我找不到使用Fragments实现这种情况的有效方法

让我们考虑一个包含1个活动的应用程序,称为A1,它控制3个片段F1,F2和F3.

F1和F2可通过NavigationDrawer访问.

当用户想要在数据库中输入数据时,F2将调用

F3.F3具有几个EditTexts和其他视图,因此在不发出警报的情况下离开F3并不是一个好主意.用户,他可能会失去一切.

在工具栏中,有一个菜单项可让用户保存数据并返回到F2.

我想控制后退按钮,以防止用户忘记保存数据或者如果您在信息方面有任何错误,我有办法向用户发送消息(Toast)数据.

仅使用活动即可执行此操作,但这不是解决方案,因为我使用的是NavigationDrawer.

据我所知,onBackPressed是一个活动回调,并且不会被Fragment拦截.

简而言之:

我想留在Fragment F3中,直到用户更正所有错误并保存数据为止.的情况下他/她在不保存的情况下轻击后退按钮,则会向其发送AlertDialog.

有人做过类似的事情吗?在Android中实现数据一致性的最佳实践是什么?

在此先感谢您的帮助.

解决方案

当用户想要在数据库中输入数据时,F2将调用

F3.F3有几个EditTexts和其他视图,所以这不是一个很好的选择在不提醒用户他可能会丢失的情况下离开F3的想法一切.

您是否考虑过备份 Fragment onPause()(甚至是 onDestroy())内的任何数据?然后,您可以将任何信息存储在 EditText 内部(可能存储到 SharedPreferences ),然后在 Fragment 重新启动(如果销毁)时将其还原./p>

此外,您可以创建一个接口(即 BackPressObserver ),并具有要对后按实现实施做出反应的任何 Fragment .

 公共接口BackPressObserver {boolean isReadyToInterceptBackPress();void onBackPress();} 

片段一

 公共类FragmentOne扩展Fragment实现BackPressObserver {@Override公共布尔isReadyToInterceptBackPress(){返回this.isVisible()&&//**另一个条件,即!editText.getText().toString().isEmpty(); **}@Override公共无效onBackPress(){//做任何需要在背压式上做的事情.完成后,请确保isReadyToInterceptBackPress()将返回false.}} 

活动

 公共类MainActivity扩展了AppCompatActivity {@Override公共无效onBackPressed(){对于(片段片段:getSupportFragmentManager().getFragments()){if(BackPressObserver的片段实例){BackPressObserver观察者=(BackPressObserver)片段;如果(observer.isReadyToInterceptBackPress()){rator.onBackPress();返回;}}}super.onBackPressed();}} 

I have programming experience, but not much in Android. I think, on any platform, we can not allow the user leave the screen without the data is saved. We also have to ensure that the data is correct before saving.

In Android, I could not find an effective way to implement this scenario using Fragments

Let's consider an app containing 1 Activity called A1 which controls 3 Fragments F1, F2 and F3.

F1 and F2 are accessed via NavigationDrawer.

F3 is called by F2 when the user wants to enter data in the database. F3 has several EditTexts and other views, so that would not be a good idea to leave F3 without alerting the user that he may lose everything.

In Toolbar there is a menu item that lets the user save the data and return to F2.

I would like to control the back button in order to prevent the user forget to save data or that I had a way to send a message (Toast) to the user, if you have any errors in information data.

Do this by using only Activities is possible, but is not a solution, because I'm using NavigationDrawer.

As far as I know, onBackPressed is an Activity callback and is not intercepted by the Fragment.

In short:

I would like to stay in the Fragment F3 until the user correct all the errors and save the data. In case of he/she tap the back button without save, an AlertDialog be sent to him/her.

Anybody did something like that? What is the best pratice to do data consistency in Android?

Thanks in advance for any help.

解决方案

F3 is called by F2 when the user wants to enter data in the database. F3 has several EditTexts and other views, so that would not be a good idea to leave F3 without alerting the user that he may lose everything.

Have you considered backing up any data inside onPause() (or onDestroy() even) of your Fragment? You can then store any information inside your EditText (perhaps to SharedPreferences), and then restore it when your Fragment restarts if it is destroyed.

That aside, you could create an interface (i.e. BackPressObserver) and have any Fragment you want to react to back presses implement it.

public interface BackPressObserver {

    boolean isReadyToInterceptBackPress();
    void onBackPress();

}

Fragment One

public class FragmentOne extends Fragment implements BackPressObserver {

    @Override
    public boolean isReadyToInterceptBackPress() {
        return this.isVisible() && 
            //**Another condition, i.e. !editText.getText().toString().isEmpty();**
    }

    @Override
    public void onBackPress() {
        //Do whatever needs to be done on a back press. When finished, make sure isReadyToInterceptBackPress() will return false.
    }

}

Activity

public class MainActivity extends AppCompatActivity {

    @Override 
    public void onBackPressed() {

        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            if (fragment instanceof BackPressObserver) {
                BackPressObserver observer = (BackPressObserver) fragment;
                if (observer.isReadyToInterceptBackPress()) {
                    observer.onBackPress();
                    return;
                }
            }
        }

        super.onBackPressed();

    }

}

这篇关于控制片段内部的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆