简体   繁体   English

横屏时,Android在finish()之后调用onCreate()

[英]Android calling onCreate() after finish() when in landscape

I have an activity that starts the Vibrator system service in its onCreate method, then when the user pushes a button it cancels the vibrator, then calls finish() to close the activity. 我有一个活动,该活动以其onCreate方法启动“振动器”系统服务,然后当用户按下按钮时,它取消了振动器,然后调用finish()关闭该活动。

This activity is brought up via the AlarmManager, so when it gets closed it will return the user to whatever app they currently had open (not necessarily mine). 该活动是通过AlarmManager引发的,因此当它关闭时,它将使用户返回到他们当前打开的任何应用程序(不一定是我打开的)。

The problem I'm having is if my activity is in landscape mode, and the user is brought to a screen that doesn't support landscape (such as the home screen) when the activity closes, my application switches to portrait and calls onCreate() before actually closing my screen. 我遇到的问题是,如果我的活动处于横向模式,并且在活动关闭时用户被带到不支持横向的屏幕(例如主屏幕),我的应用程序将切换为纵向并调用onCreate( ),然后再实际关闭屏幕。 So the steps causing this problem are as follows... 因此,导致此问题的步骤如下...

  • The activity is lauched in portrait mode 活动以肖像模式启动
  • onCreate method gets called, which starts the vibrator 调用onCreate方法,这将启动振动器
  • The user rotates the phone to landscape mode 用户将手机旋转到横向模式
  • onCreate is called again, but because onSaveInstanceState is not null, I can skip starting the vibrator again 再次调用onCreate,但是因为onSaveInstanceState不为null,所以我可以跳过再次启动振动器的操作
  • The user pushes the button to close the screen 用户按下按钮以关闭屏幕
  • I call vibrator.cancel() 我叫vibrator.cancel()
  • I call finish() 我打电话给finish()
  • Because the screen the user will be brought back to a screen that doesn't support landscape mode, my activity calls onCreate() 因为屏幕将使用户返回到不支持横向模式的屏幕,所以我的活动将调用onCreate()
  • savedInstanceState equals null, so the vibrator gets started again savedInstanceState等于null,因此振动器重新启动
  • My app is closed with the vibrator still running 我的应用已关闭,但振动器仍在运行

Currently the only way I can think of to rectify this is to make my activity only support portrait mode, but I'd like to avoid that if I can. 目前,我想纠正这一问题的唯一方法是使我的活动仅支持人像模式,但我想避免这种情况。 Does anyone know a way in which I can prevent onCreate() from being called after I call finish()? 有谁知道我可以在调用finish()之后阻止onCreate()的方法吗?

why dont you call method 你为什么不叫方法

onPause() or onStop() and inside this methods call vibrator.cancel() onPause()onStop() ,在此方法内部调用vibrator.cancel()

onStop() always gets called when your app is not visible anymore. 当您的应用程序不再可见时,始终会调用onStop()。

check out this flowchart 查看流程图

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM