如何prevent意外退出应用程序瓦特/ Android中片段/活动? [英] How to Prevent Accidental App Exit w/in Android Fragments/Activities?

查看:161
本文介绍了如何prevent意外退出应用程序瓦特/ Android中片段/活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何prevent意外退出应用程序瓦特/ Android中?

How Do I Prevent Accidental App Exit w/in Android?

IE浏览器:当他/她presses后退按钮,并达到在BackStack的最后一项活动,展现举杯问,如果用户想使用pssed()方法onBack $ P $退出

IE: When the he/she presses the back button and reaches the last activity in the BackStack, show a toast to ask if the user wants to exit using the onBackPressed() method.

此外,它需要与backStacks的片段的功能。

Also, It needs to function with backStacks for fragments.

推荐答案

该功能可以很容易地通过覆盖主要活动的onBack pressed()方法来实现。在这个例子中,当用户presses回按钮,然后应用程序将显示举杯4秒这一次新的回preSS立即终止应用程序。

This functionality can easily be implemented by overriding main activity's onBackPressed() method. In this example when user presses back button then the app will display a toast for 4 seconds on which time a new back press terminates the app immediately.

REF

您可以把它放在一个BaseActivity扩展活动是这样的:

You can put it in a BaseActivity that extends Activity like this:

public class BaseActivity extends Activity{

    private Toast toast;
    private long lastBackPressTime = 0;
    . . .

    /**
     * Prevent accidental app exit by requiring users to press back twice when
     * the app is exiting w/in 4sec
     */
    @Override
    public void onBackPressed() {
      if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
        toast = Toast.makeText(this, "Press back again to close this app", 4000);
        toast.show();
        this.lastBackPressTime = System.currentTimeMillis();
      } else {
        if (toast != null) {
        toast.cancel();
      }
      super.onBackPressed();
     }
    }
    . . . 
}

编辑:添加片段BACKSTACK兼容性

有关使用来检测一个bacstack卫生组织的应用程序的最后一个片段单独使用碎片,我强烈建议把你的dispatchKeyEvents在BaseActivity类并实现上述方法,像这样:

For use to detect the last fragment in a bacstack whos application is solely using fragments, I strongly suggest putting your dispatchKeyEvents in a BaseActivity class and implementing the above method like so:

public class BaseActivity extends Activity {

    public boolean dispatchKeyEvent(KeyEvent event) {
        int backCount = getFragmentManager().getBackStackEntryCount();
        int action = event.getAction();
        int keyCode = event.getKeyCode();

        FragmentManager fm = getFragmentManager();

    . . .

        case KeyEvent.KEYCODE_BACK :
                if (action == KeyEvent.ACTION_DOWN && backCount == 0) {
                    onexitNotify();
                }else {
                    fm.popBackStack();
                }
                return true;

            default :
                return super.dispatchKeyEvent(event);
        }
    }

/**
 * Prevent accidental app exit by requiring users to press back twice when
 * the app is exiting w/in 8sec
 */
    private Toast toast;
    private long lastBackPressTime = 0;

    public void onexitNotify() {
        if (this.lastBackPressTime < System.currentTimeMillis() - 8000) {
            toast = Toast.makeText(this, "Press back again to close this app", 8000);
            toast.show();
            this.lastBackPressTime = System.currentTimeMillis();
        } else {
            if (toast != null) {
                toast.cancel();
            }
            super.onBackPressed();
        }
    }
}

*如果您使用的是2.0+,onBack pressed()简化必要的,这样的onkeydown()是不需要的。的code量

*If you're using 2.0+, onBackPressed() simplifies the amount of code needed so onKeyDown() is not needed.

每androd模式recomendation:

Per androd patterns recomendation:

有些应用程序提示用户,当它即将退出。这似乎   与游戏应用程序尤为常见。这种做法是不   建议在正常的应用程序。将确认提示   扰乱用户的正常工作流程。即使在使用这个选项的游戏   经过仔细考虑的。

Some applications prompt user when it is about to exit. This seems to be particularly common with game apps. This practice is not recommended in normal applications. A confirmation prompt would disrupt user's normal workflow. Even in games using this option should be very carefully considered.

这篇关于如何prevent意外退出应用程序瓦特/ Android中片段/活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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