gpt4 book ai didi

android - Activity noHistory ="true"在屏幕锁定时不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:01:09 25 4
gpt4 key购买 nike

我已经为 list 上的 Activity 设置了 noHistory="true"。预期结果是当用户离开 Activity 并且它在屏幕上不再可见时,Activity 将完成。

当我按主页按钮导航到不同的应用程序并按预期返回 Activity 重新创建时,这工作正常。但是当 Activity 可见时,如果屏幕锁定,然后解锁,恢复 Activity 。我希望 Activity 重新创建或只是不显示,让用户再次启动应用程序。

  <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".TestActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

I want the Activity to recreate or just not show up, let the user launch the app again.

我相信您可以处理如果屏幕被锁定,然后执行您的操作,例如终止应用程序。好像当用户锁定屏幕时, Activity 进入 onPause(); 方法。

这是日志:

02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onCreate() event
02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event
02-18 20:27:23.626 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event
02-18 20:27:27.156 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onPause() event
02-18 20:27:27.161 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStop() event
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onRestart() event
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event
02-18 20:27:36.881 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event

所以,当用户像这样在 onPause(); 方法中锁定屏幕时,终止应用程序/或做你的事情:

public void onPause() {
super.onPause();

Log.e(tag, "In the onPause() event");
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn(); // deprecated, but you can use isInteractive too

if (!isScreenOn) {

finish(); // or do your stuffs
}

}

然后,如果用户锁屏,App就结束了,需要重新打开。

关于android - Activity noHistory ="true"在屏幕锁定时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com