0

My app interfaces with a Bluetooth peripheral. When the peripheral wants to shut down, can I clean up the app simply by calling my Activity's own onPause() and onStop() methods? Is the fact that they call the superclass's methods likely to cause any problems?

The idea would be to call finish() after that.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43

3 Answers3

1

Technically, yes, you can. What you should be asking is "Should I"? Which the answer is no.

Like you've mentioned, since they do call the superclass methods, there is some extra Android OS cleanup magic that happens. It may result in a successful case once in a while, but it's not guaranteed. There is a lot of things that happen in the backround that you don't want to fool with. Don't reinvent the wheel.

If there is code that is ran within the onPause and onStop methods that you would like to use elsewhere, I would create a function called cleanupBluetooth and the onPause and onStop would call and anywhere else it needs to.

If you need to actually call the onPause and onStop methods because you need to stop and halt the activity, you can do that by calling finish() (how to use finish()). The finish() method will call the appropriate Android OS magic that's needed to be called.

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
0

You can call them manually, but you shouldn't let Android do it itself when it needs to. If you wish to call those activities, you are likely not really wanting the activity any more, so you can just call finish and then android will call the relevant methods based on the activities life cycle.

You can find more info about the activity life cycle by going here

Boardy
  • 35,417
  • 104
  • 256
  • 447
0

OnPause() is also a method in your activity and it will likely normal method. It overrides method of activity and you can call it from anywhere in activity and from interface also.

Harsh Mittal
  • 449
  • 3
  • 10