Search code examples
androidback-button

Use another button instead of back button


I have a problem with using back button in android. I know I can use onBackPress() method to handle it, but I need a way to do the event of the back button inside onResume lifecyle. The reason why I have to do it this way, is I have started a timer in another class from the onResume method. And when I press the back button this timer should stop. But when I use the onBackPress() method to stop it I got a NullPointerException because the timer never started. When I use a button to stop the timer which is inside the onResume method then the timer stops correctly. So the quastion is can I use another button instead of back button? Or how can I use the event of the back button inside onResume when the user press it?


Solution

  • no need to do this kind of extra work, if you need all this things just because of timer nullpointer write below condition before stopping timer.

    make your timer variable public static and then

    if(YouActivity.timer!=null)
       YouActivity.timer.stop();