Groups
Groups
Sign in
Groups
Groups
日本Androidの会
Conversations
About
Send feedback
Help
ApplicationクラスのSingleton化について
1,699 views
Skip to first unread message
suppi__
unread,
Oct 7, 2012, 12:35:19 AM
10/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
suppi__です。
AndroidのApiではパラメータにContextが必要な物が多いと思いますが、
Utilityクラスなどを作成した際にパラメータにわざわざContextを渡したくないです。。
Applicationクラスを継承したクラスをSingletonにしてContextの代わりに使用したいと思っているのですが、
なにか問題点はありましたら教えていただけますでしょうか?
よろしくお願いします。
XBG
unread,
Oct 7, 2012, 3:06:51 AM
10/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
Applicationを使ったところであまり意味はないです。
どのみちContextは必要になるので。
suppi__
unread,
Oct 7, 2012, 4:52:49 AM
10/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
XBG様ありがとうございます。
言葉足らずで申し訳ございません、、ApplicationクラスをSingleton化することでアプリが
常に終了しなくなったり、メモリリークなどの問題が発生するかを教えていただきたかったのです。
リソースの取得などは、ApplicationからApplicationContextが取得できるので
問題なく動作すると思います。
よろしくお願いします。
2012年10月7日 16:06 XBG <
hrsn...@gmail.com
>:
> Applicationを使ったところであまり意味はないです。
> どのみちContextは必要になるので。
>
> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このディスカッションをウェブ上で閲覧するには、
https://groups.google.com/d/msg/android-group-japan/-/4XihtoNJPVIJ
にアクセスしてください。
> このグループに投稿するには、
android-g...@googlegroups.com
にメールを送信してください。
> このグループから退会するには、
android-group-j...@googlegroups.com
にメールを送信してください。
> 詳細については、
http://groups.google.com/group/android-group-japan?hl=ja
からこのグループにアクセスしてください。
>
河村潔広
unread,
Oct 7, 2012, 1:02:58 PM
10/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
kyorohiroです。
念のため、弱参照でApplictionインスタンスを保持するというのは、どうでしょうか?
「メモリリークなどの問題」の心配をしなくて良くなります。
また、 私もよくUtilityクラスなどで、ApplicationクラスをSingletonで使用しています。
しかし、「常に終了しなくなったり」といった事が起きたことはありません。
以上です。
2012年10月7日 17:52 suppi__ <
harmony...@gmail.com
>:
Makoto Yamazaki
unread,
Oct 7, 2012, 9:46:24 PM
10/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
zaki です。
アプリが動いている間は Application オブジェクトは常に存在しているので
Application 自身のリークの心配は無いとおもいます。
ただ、Application、Activity、Service それぞれで Context が微妙に違うので
すべてを Application にしても OK というわけではないので注意が必要です。
たとえば、Application と Activity では指定されているテーマが違う場合があるのと、
ライフサイクルが異なるのでどちらに対して registerReceiver するかで
BroadcastReceiver のライフサイクルが変わったりします。
そのため渡すのが面倒という理由だけで常に Application の方を使うというのは
正しくなく、それぞれのユーティリティクラスでどちらを使うべきかを検討すべき
と思います。
Googleが出しているリファレンスの記述では、"This generally should only be used
if you need a Context whose lifecycle is separate from the current
context" (ここでの
This は ApplicationContext の事) となっているのでこれを基準に判断すればいいかなと
思っています。
参考になりそうなURLをあげておきます。
http://android-developers.blogspot.jp/2009/01/avoiding-memory-leaks.html
http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29
2012/10/7 suppi__ <
harmony...@gmail.com
>:
> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループに投稿するには、
android-g...@googlegroups.com
にメールを送信してください。
> このグループから退会するには、
android-group-j...@googlegroups.com
にメールを送信してください。
> 詳細については、
http://groups.google.com/group/android-group-japan?hl=ja
からこのグループにアクセスしてください。
>
--
YAMAZAKI Makoto
suppi__
unread,
Oct 8, 2012, 3:33:14 AM
10/8/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-g...@googlegroups.com
kyorohiro様、zaki様返答ありがとうございます。
実際に使われていることを知って安心しました。
ライフサイクル周りに気をつけて使いたいと思います。
現状リソースの取得と、SQLiteOpenHelperあたりを切り離したいと考えていたので
問題無さそうでよかったです。
2012年10月8日 10:46 Makoto Yamazaki <
makot...@gmail.com
>:
Reply all
Reply to author
Forward
0 new messages