ã¿ã¤ãã«ã¯èªå¼µã§ãã
ã¿ããªå¤§å¥½ãpotatotipsã¨ããåå¼·ä¼ã§ä»¥ä¸ã®ãããªãã¼ã¯ããã£ãããã§ãã(ç¾å ´ã«ã¯è¡ã£ã¦ãã¾ãã)
æ°ããActivityContextã§ä½ååããã£ããæ°åãªã¼ã¯ãæ¤åºãããã ãªãã¹ãApplicationContextã使ãããã¨ã®ãã¨ãããã§ãããééããªãæ»äººãåºã(èªå¼µ)ã¨æãã¾ãã®ã§ã注æãã ããã
(追è¨20160527: ä¿®æ£ãããããã§ã ãªãããã¿ã¾ãã) speakerdeck.com
çµè«
Application Contextã¯ä½¿ããªãã§ãã ããã(èªå¼µ)
(å®éã¯ä½¿ãã¨ãã¯ä½¿ã)
ãªãApplication Contextãªã®ã
Application Context使ããã!ã¨ããæ ¹æ ã®è¨äºã¨ãã¦ãã¡ãã®ãã§ã³ãªã«ã®Developer's Blogã® [追記あり] Android 開発で気をつけたいこと 〜変数名と Context について〜 (フェンリル | デベロッパーズブログ) ã¨ãã2012å¹´3æã®è¨äºã2å¹´ã«1åããã話é¡ã«ãªãã¨æãã¾ãã
ããã¦ãã®æ ¹æ ã¨ãã¦ãããããã®ã2009å¹´1æã®Android Developersã®ããã° Avoiding memory leaks | Android Developers Blog ã§ãã
ãããã«æå¾ã®ã¾ã¨ãã«ã¯
ã»Try using the context-application instead of a context-activity
è¶ è¨³) Application Contextã使ãã!
ã¨ãããããã«ãApplication Contextãæ£è§£ãã®ããã«è¦ããªããã¨ããªãã§ãã
ãã®è©³ç´°ããã®å段ã«ãããéã«è¨³ãã¨ãããªæãã®ãã¨ãæ¸ããã¦ãã¾ãã
Contextã«é¢é£ããã¡ã¢ãªãªã¼ã¯ãé¿ããç°¡åãªæ¹æ³ã2ã¤ãã£ã¦ã1ã¤ã¯ãã®Contextããã®ã¹ã³ã¼ãå¤ã§ä½¿ããªããã¨ããã1ã¤ã¯Application Contextã使ããã¨ããã®Contextã¯Activityã®Lifecycleã«ä¾åããªããé·çããããªãã¸ã§ã¯ãã§Contextãå¿ è¦ãªã¨ãã¯Applicationãªãã¸ã§ã¯ãæãåºãã°ãk
ãã®2ã¤ã¯æ¬è³ªçã«ã¯åããã¨ãè¨ã£ã¦ãã¦ãã¹ã³ã¼ãèãã¦ä½¿ãããâ¦ãã§ãããApplication Contextæé«! ãã使ã£ã¦ããã°ã¡ã¢ãªãªã¼ã¯ã«ãµã¨ãã©!ãã¨ã¯è¨ã£ã¦ãã¾ããã
ã¡ãªã¿ã«ãä¸è¨ãã§ã³ãªã«ã®ããã°ã®
Context ã®å¼æ°ã¨ãã¦ä»¥ä¸ã®ããã« Activity èªèº«ã MainActivity.this ã®ããã«ãã¦æ¸¡ãã¦ããä¸è¦åé¡ãªãåä½ããã®ã§ãããã¡ã¢ãªã¼ãªã¼ã¯ãããå±éºæ§ãããã¾ãã (åè URL : http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html )
new Intent(MainActivity.this, FooActivity.class);
ç¹ã«çç±ããªãå ´åã¯ã以ä¸ã®ããã« this ã§ã¯ãªã getApplicationContext ã使ç¨ããæ¹ãç¡é£ã§ãããã¡ã¢ãªãªã¼ã¯ãèµ·ãããã¨ãåé¿ãããã¨ãã§ãã¾ãã
new Intent(getApplication(), FooActivity.class);
ã«å¯¾ãã¦ã¯ä»¥ä¸ã®ããã«æ確ã«å¦å®ããã¦ãã¾ã
ç¥ãåãã«èãããhttp://t.co/JTzInmjeKI
— Yuki Anzai (@yanzm) June 25, 2015
ã«æ¸ããã¦ããã¡ã¢ãªãªã¼ã¯ã®ä¾ã®ãã¨ã§ãã
ã¯ã£ããè¨ã£ã¦ãã®ä¾ã¯ãã³ã»ã³ã¹ã§ãã
Intentã®ã³ã³ã¹ãã©ã¯ã¿ã«æ¸¡ãContextã¯ãããã±ã¼ã¸åï¼Stringã ãï¼ã®åå¾ã«ãã使ããã¦ãã¾ããã
ãã ãããã ãã§ã¯ãããããã£ã±ããã¤ãã¹ã³ã¼ãåºãApplication Contextã§ããããããã¨è¨ããªãããªãã§ãããããããããªãã§Application Contextã¨Activity Contextãããã®ããã¨ããã®ãèãã¦ã¿ã¾ãããã
ãªãActivity Contextãªã®ã
èãã¾ããã?
ä»ã±ã£ã¨åºã¦ããã«èããããªãã¯Android 2ç´ã§ããRxãããå
ã«å¦ã¶ã¹ããã¨ãããã¾ãã
ã¨ããã®ã¯åååè«ã§ã:bow:ãã
å
·ä½çãªApplication Contextã¨Activity Contextã®éãã«ã¤ãã¦ã¯ãã¡ãã®2012å¹´2æã®è¨äºã«ãã®ããã¼ãããæãã«å®éã®çµæã¨ã¨ãã«æ¸ãã¦ããã¾ãã®ã§å¾¡è¦§ãã ããã
Yukiの枝折: Android:引数はthisか?getApplicationContextか?ActivityとApplicationの違い
ã¤ã¾ãã¨ããActivityã«ã¯ããããã®themeãªãããããã®ã§é©å½ã«ãã£ã¦ãã¨æã£ãéãã«ãªããªããã¨ãããã©ããããä¸è¨è¨äºã®ããã«ææªã¯ã©ãã·ã¥ãã¾ãã
ã¾ãããã¯åããã¦ã¿ãã°ãããããã®ã§ã大ããåé¡ã§ã¯ç¡ãããã«æãããããããã¾ãããã
Contextã®éãã¤ãã¦ãããå°ãé·ããå ·ä½çãªã·ãã¥ã¨ã¼ã·ã§ã³ã®èª¬æãAndroid Developersã®Google+ã®ãã¹ãã«ããã¾ãã2012å¹´3æã®æ稿ã§ãã
plus.google.com éã«è¨³ãã¨ã
Viewãã¤ããã®ã«Application Contextã使ãã®ãçã«ãªã£ã¦ããªããæã£ãéãã®ãã®ãåºæ¥ä¸ãããªãããªãã¦ãã¨ã«ã¶ã¡å½ããã¾ã§ããé·ãã¯ãªãã ãããã§ãApplication Contextãã¡ã ã£ã¦è¨ããªãä½ã使ãã°ãããã ? ããã¯ãã®Viewãã©ãã«ç½®ãã次第ã ã
ãActionBar
ã«ã¯getThemedContext()
ããã£ã¦action barã«é©åãªthemeãã¤ãããã¤ã使ãããTheme.Holo.Light.DarkActionBar
ãªããã¯Activityã®ã³ã³ãã³ãé¨åã¨ã¯å ¨ãç°ãªãè¦ãç®ã«ãªãã
ãDialog
ã«ã¯dialog windowã«å¿ è¦ãªthemeã使ããgetContext()
ããããAlertDialog.Builder
ã使ã£ã¦ããªãViewã®inflateæã«ã¯Dialog
ãªãã¸ã§ã¯ãã¯ãªãã ãããããAPI11ããgetContext()
ã追å ããã
ãä»ã«ã¯ãµããã¤ã³ã¨ä»ã¨ã§å¤§ããç°ãªãè¦ãç®ã«ããã®ã«ãContext
ã使ãããContextThemeWrapper
ã£ã¦ã®ã§Context
ããããã§LayoutInflater
ãªããã«æ¸¡ãã°ããã
ããããã使ãåããã°æé«ã®ã¢ããªãä½ãã®ã楽ã«ãªãã¯ãã ã
(詳ããã¯åæãããã£ã¦ãã ãã)
ã¤ã¾ããç¹ã«Viewã«é¢é£ããã¨ããã§Application Contextã使ãã®ã¯ã¢ã³ããã¿ã¼ã³ã§ãã(ä¸è¨ã®ä¾ã§è¨ãã°Activity Contextãé©åã§ãªããã¿ã¼ã³ã¾ã§çºå±ãã¦ã¾ãã)
ããªãã
AndroidManifest.xml
ã®<activity>
ã«ä¸åº¦ã§ãandroid:theme
ã¨æ¸ãããã¨ãããã®ã§ããã°Application Contextã使ãã®ãããã¾ããããandroid:theme
ã<application>
ã«ããæ¸ãããã¨ããªã人ãä»ã®ãã¡ã«ãããã»ããè¯ãã§ãããã
çµè«
ç¹ã«çç±ããªãã¨ãã¯Application Contextã§ã¯ãªãActivity Contextã使ãã¾ããã
- ãã®ä»ã®ãªã½ã¼ã¹