Use a persistent notification to allow the user to return to running Android app

后端 未结 5 784
情歌与酒
情歌与酒 2020-12-30 10:22

I am developing an app with numerous Activities. I would like to create a persistent notification that (more or less) says, \"AppName - Return to AppName\" that will be pre

5条回答
  •  醉酒成梦
    2020-12-30 10:41

    Okay, I believe that I have found a satisfactory work-around for my specific case. I've added a static integer to my "mainActivity", and each time it's "onCreate" is fired, it increments the integer. Each time it's "onDestroy" is fired, it decrements.

    In my "returnFromNotify", I look at the static integer to see if it is greater than 0. If so, I assume there is an active "mainActivity", and that running "finish" inside "returnFromNotify" will return there. Otherwise, it assumes the users has "backed" out, finishes itself, then uses "startActivity" to fire up a new instance of "mainActivity".

    This is not a universal solution, but for my purposes, I think it will suffice. I am still open to other answers, and if someone can punch a hole in my logic, please do so - constructive criticism is welcome. Thanks.

提交回复
热议问题