Search code examples
androidandroid-activityactivity-lifecycle

Activity.isFinishing() return true but is wrong


Before I show a dialog I check if activity is finishing to avoid the app crash.

if(!activity.isFinishing()){
    showDialog();
}

but on some cases it returns true but the activity is not finishing and I can continue using my app but when I try to show a dialog it says the activity is finishing.

In what scenario can it happen?


Solution

  • I found out what was the problem for me.

    the class that checked if(!activity.isFinishing()) is a Singletone and in some cases when the app went to the background the Activity was closed and when the app came back to foreground a new Activity was created and the old activity was still in the Singletone and wasn't updated.

    so I made sure the Singletone updated the Activity in that case.

    hope it helps anyone.