Singletons vs. Application Context in Android?

后端 未结 10 1418
醉话见心
醉话见心 2020-11-22 03:11

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it\'s a go

10条回答
  •  离开以前
    2020-11-22 03:42

    I had the same problem: Singleton or make a subclass android.os.Application?

    First I tried with the Singleton but my app at some point makes a call to the browser

    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    

    and the problem is that, if the handset doesn't have enough memory, most of your classes (even Singletons) are cleaned to get some memory so, when returning from the browser to my app, it crashed everytime.

    Solution: put needed data inside a subclass of Application class.

提交回复
热议问题