In the latest version of my application, some users experience a failure that I cannot reproduce. Currently, only Samsung devices running Lollipop have a problem, but it might just be a coincidence. After analyzing the stack trace and related code, I think I might have found the culprit. To test my assumption, I simplified the code below:
public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button b = new Button(this); b.setText("Click me!"); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Handler().post(new Runnable() { @Override public void run() {
Each time I test the above application, first clicking the "Click me" button and then the "Back" button, listenerNotified is printed on the console before onDestroy() .
However, I'm not sure if I can rely on this behavior. Does Android provide any warranties in the above scenario? Is it safe to assume that my Runnable will always be executed before onDestroy() or is there a script where this is not so? In my real application, of course, much more happens (like other threads sent to the main thread, and more operations happening in the callback). But this simple passage seemed sufficient to demonstrate my concern.
Is it possible (possibly due to the influence of other threads or callbacks sent to the main thread) that I get the debug output below?
D/TAG: onDestroy D/TAG: listenerNotified
I would like to know this, as this result would be possible, explaining the failure.
s1m0n source share