Use onstop() callback method in Android to recover activity state if destroyed accidently

2 minutes read

 Use onstop() callback method in Android to recover activity state if destroyed accidently

 

In android system, activity gets destroy in two scenarios.

 

 

1. When user press back button or write code in activity to destroy itself.
2. When activity is currently in paused/stopped state and it is not used in for a long time and current foreground activity requires more memory or resources.

 

 

 

Android system forcibly killed that activity and store it’s all member variables values in a bundle. If user destroy activity according to his/her wish then it doesn’t matter but if android system kills that activity without knowing to user, it may lead to app crash. To avoid such a situation programmer should use smart trick. For programmer it is necessary to implement that trick in his/her coding.
He/She should use onstop() callback method to release all occupied system resources and let the activity to use those resources and also implement onSaveInstanceState() callback method to store all member variables values into android system’s bundle.
When activity recreates itself then data can be recovered by that bundle. It will help to programmer to regain data in case when android system kills your activity. Through this smart trick programmer can avoid accidental app crash and also can make it to more effective in such accidental cases.

 

 

Related Posts...

GeneralTechnologies

Tags: