简体   繁体   English

按下最近的应用程序按钮时清除EditText

[英]Clearing an EditText when the recent apps button is pressed

I want to clear the edit text which is basically a password field when app goes to background or when recent apps button in menu is pressed. 当应用程序进入后台或按菜单中的“最近使用的应用程序”按钮时,我想清除基本上是密码字段的编辑文本。

I do this way - 我这样做-

@Override
public void onPause() {
    super.onPause();
    if (password != null) {
        password.getText().clear();
    }
}

But my problem is when the recent apps button in menu is pressed, the app goes to half background and I can see the password still. 但是我的问题是,当按下菜单中的“最近使用的应用程序”按钮时,该应用程序将变为一半背景,并且仍然可以看到密码。 How do I clear it in this scenario? 在这种情况下如何清除?

Do the above code inside onStop() , onPause() , onDestroy() ie all the possibe functions that android can call when the app goes in background. onStop()onPause()onDestroy()执行以上代码,即当应用程序进入后台时,android可以调用的所有possibe函数。

Many times onStop() method is called instead of onPause() 多次调用onStop()方法而不是onPause()

None of standard Activity Lifecycle methods is called when "Recent Apps" button pressed. 当按下“最近的应用程序”按钮时,不会调用标准的Activity Lifecycle方法。 Activity will stay active after list of recent apps popups. 在列出最近的应用程序弹出窗口后,活动将保持活动状态。

The only Activity method are getting called when user opens "Recent Apps" list (or returns from it) is onWindowFocusChanged with boolean parameter hasFocus . 当用户打开带有布尔参数hasFocus onWindowFocusChanged (或从中返回)“最近的应用程序”列表(或从中返回)时,将调用唯一的Activity方法。 When user open list of Recent App method onWindowFocusChanged() called with hasFocus equals false , and same method called with hasFocus equals true when user pressing Back in this list. 当用户打开使用hasFocus调用的“最近的应用程序”方法onWindowFocusChanged()列表等于false ,并且当用户在此列表中按Back时,使用hasFocus调用的相同方法等于true

Try below code, 试试下面的代码,

public void onWindowFocusChanged(boolean hasFocus) {         
    super.onWindowFocusChanged(hasFocus);
    if(!hasFocus && password != null) {
        password.setText("");
    }
}

But my problem is when the recent apps button in menu is pressed, the app goes to half background and I can see the password still. 但是我的问题是,当按下菜单中的“最近使用的应用程序”按钮时,该应用程序将变为一半背景,并且仍然可以看到密码。 How do I clear it in this scenario? 在这种情况下如何清除?

As the official documentation says: 如官方文档所述:

onPause() execution is very brief, and does not necessarily afford enough time to perform save operations. onPause()的执行非常简短,不一定提供足够的时间来执行保存操作。 For this reason, you should not use onPause() to save application or user data, make network calls, or execute database transactions; 因此,您不应使用onPause()保存应用程序或用户数据,进行网络调用或执行数据库事务。 such work may not complete before the method completes. 在该方法完成之前,此类工作可能无法完成。 Instead, you should perform heavy-load shutdown operations during onStop() 相反,您应该在onStop()期间执行重负载关闭操作

So you should move the code to onStop() 因此,您应该将代码移至onStop()

And if you wanna know how and why kindly check: https://developer.android.com/guide/components/activities/activity-lifecycle 如果您想知道如何以及为什么请检查: https : //developer.android.com/guide/components/activities/activity-lifecycle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮时清除edittext - clear edittext when button is pressed 按下按钮时新建EditText - New EditText when button pressed 在android中按下导航后退按钮时保留edittext数据 - preserve edittext data when navigation back button pressed in android 按下按钮时清除编辑文本焦点并隐藏键盘 - Clear edittext focus and hide keyboard when button is pressed 当 EditText 为空白并按下按钮时,Android 应用程序崩溃 - Android app crashes when EditText is blank and button pressed 清除最近的应用程序(长按主页按钮时) - Clear recent apps(when long click home button) 我正在搜索用于清除后台运行应用的代码,并从最近的应用菜单中删除所有其他应用 - I am searching a code for clearing background running apps and remove all other apps from recent apps menu 在按下返回按钮后再次创建活动时,EditText返回相同的值 - EditText returning same value when activity is created again after the back button is pressed 我的问题是,当按下按钮时,为什么我输入的编辑文本中的文本不会显示在另一个文本视图中? - My question is why won't my inputted text into an edittext show up in another textview when a button is pressed? 按Enter键或按钮时,如何将EditText中的文本打印到textview - How do I print text from EditText to a textview when pressed enter or a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM