0

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()?

Rabbit
  • 115
  • 2
  • 10

1 Answers1

0

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

Eliud
  • 61
  • 5
  • That works, but it's defiantly not optimal. When I do it that way, I click the button to stop the vibrator, it stops, then when I call finish it starts again for about 3 seconds, then stops again (apparently that's the time it takes for android to call onCreate and onStop after I've called finish(), at least on my phone). – Rabbit Feb 22 '13 at 17:53
  • what about onPause that is faster than onStop(). – Eliud Feb 22 '13 at 18:17
  • @eluid If I move my code to start the vibrator to onResume and then put the cancel in onPause, it seems to be working alright. Thanks for your help. – Rabbit Feb 22 '13 at 20:38