Question

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.

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).

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. So the steps causing this problem are as follows...

  • The activity is lauched in portrait mode
  • onCreate method gets called, which starts the vibrator
  • 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
  • The user pushes the button to close the screen
  • I call vibrator.cancel()
  • I call finish()
  • Because the screen the user will be brought back to a screen that doesn't support landscape mode, my activity calls onCreate()
  • savedInstanceState equals null, so the vibrator gets started again
  • 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()?

Était-ce utile?

La solution

why dont you call method

onPause() or onStop() and inside this methods call vibrator.cancel()

onStop() always gets called when your app is not visible anymore.

check out this flowchart

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top