Issue
I have an android app in which first when the user opens my app I show the Splashscreen and after that user is navigated to HomeActivity.
Now actually I saw a strange thing happening which is before ondestroy() function of my Splashscreen I am calling a function which is calling a Toast and making progressBar GONE but the thing is, I mistakenly called this function in a loop so what is happening is my Splashscreen ondestroy function got called, my HomeActivity is showing but still Toasts are being shown from Splashscreen again and again.
Now my question is as after destroying the Splashscreen why android is not giving me the error because as I am referencing Toast and ProgressBar which is bound to the Splashscreen so it must give me the error.
And also if it should not give me the error then please tell me what is the concept which is working here and I don't know. Isn't all variables which we make in our activity class gets bound only to that activity and when that activity gets destroyed so does them ??
My Splashscreen function code is given below which is getting called again and again
public void function showdialog() {
Toast.makeText(getBaseContext(), "Total Pending Notifications: " + pen_notifs, Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.VISIBLE);
}
Thanks in Advance.
Solution
I went through the source code and this is my theory.
Toast internally uses Service (Notification service) to queue and show the Toast views. Service runs in background and its lifecycle outlives any Activity's.
The Notification Service has a reference to the Toast view that you are created using the makeText() method.
And the Toast view has an implicit reference to the context which is your Activity's context.
So since the Notification service has a reference to your Toast view and the toast view has a reference to your Activity, your Activity hasn't been destroyed at all. Your Activity is leaking. Since your Activity hasn't been destroyed, you are not getting any NullPointerExceptions even if the progressBar is accessed after the onDestroy() callback.
The for loop has been running continuously and the Notification service implicitly holds a reference to your Activity. So there is no chance for your Activity to get destroyed.
Answered By - Bob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.