android application behaves indifferently when app is not used for a while
The app works fine when called afresh and used, but it doesn't behave well when user leaves it and the application is left running in the memory. Any attempt to switch back to it when it is running causes crash or black frozen screen. It might happen only when some time is left since it was opened first. To 开发者_如何转开发solve this, I am thinking of placing a Quit button and used this to kill the app
case MENU_QUIT: int pid = android.os.Process.myPid();
System.out.println("Process id:"+pid);
android.os.Process.killProcess(pid);
return true;
But I believe this is not the right way to solve the problem. Could somebody suggest a better idea.
Those kinds of crashes and freezes can happen if you don't implement the process lifecycle callback methods correctly.
Implement your activity's onPause method. Make it stop active processing threads, save what needs saving and stop processing (Task killers may kill you at any time so onPause might be where the app terminates).
Implement onResume in a way to reload whatever state the app was in and pick up where it left off.
Look at your code in onStart. Does it differ a lot from onResume? Perhaps that will narrow down where your state went awry. If onResume reinitializes something wrong or fails to reacquire some resource bad things can happen).
精彩评论