简体   繁体   中英

Android: Calling finish sometimes closes whole app

I have this strange behavior. My app has several activities that the user can navigate back and forth between. The app starts with a spash screen, and after a few seconds you get lunched into the main activity. The splash screen is then removed from the stack.

If the user presses the "settings" button, a new activity is launched. Every now and then when the user navigates back from the settings activity (or any other activity and wanting to return to the main activity), the app shuts down. No exceptions in the catlog what so ever (Yes, i removed filter to show all the lines from the entire system). This happens on multiple different devices with different Android version, but not all the time. I cannot find a pattern,

Sometimes it will work as expected for hours at end, then it will happen 2-3 times in a row.

I'm using a button on the bottom of the screen to navigate back and on the Click-event i simply call "finish()" All activities is launched with "startActivity( intent )".

I have no idea how to continue to search for the reason when i cannot get any exceptions or warning at all. Do anyone have any idea to what's going on or how to continue my investigation?

Sometimes it will work as expected for hours at end, then it will happen 2-3 times in a row.

Android activity is very unpredictable entity. You see, OS can destroy activities in the background at its own discretion (for example, the lack of resources). So when you leave your activity you can't be shure, that it's will e alive, when you will come back on it. There are few ways to handle this problem.

I can advice you the current ones:

  1. Back to the prev activity directly (open it, after your current activity finished)
  2. (Advice/Best parctice) - Dont use activities so often! Please, use Fragments for your tabs screen. You should have only 1-3 activities in your application and N Fragments on them. Believe me, working with Fragment is much easier, productive and safe. And of course, Google recommend to use Fragments instead of Activities.

Ps - read this https://medium.com/mobile-app-development-publication/activities-or-fragments-a-little-sharing-c1ddc1041f79 It will help you to understand my answer fully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM