{"id":344,"date":"2013-03-04T06:33:07","date_gmt":"2013-03-04T11:33:07","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=344"},"modified":"2013-03-04T06:33:07","modified_gmt":"2013-03-04T11:33:07","slug":"techniques-for-testing-android-fragments","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2013\/03\/04\/techniques-for-testing-android-fragments\/","title":{"rendered":"Techniques for testing Android fragments"},"content":{"rendered":"<p>How to test a fragment-based Android application:<\/p>\n<ul>\n<li>Switching fragments directly from a sub-fragment (inside backstack) &#8211; This tests how well your fragment switching code deals with backstacks while switching fragments.<\/li>\n<li>Switching from another top level fragment &#8211; This tests your fragment switching code.<\/li>\n<li>Turning off the screen &#8211; This tests your onResume() and onResumeFragments() handlers<\/li>\n<li>Opening another activity from a fragment &#8211; Same as above<\/li>\n<li>Opening another activity from the action bar &#8211; Same as above<\/li>\n<\/ul>\n<p>Preferably test in the &#8220;don&#8217;t keep activities&#8221; mode, as this will root out a ton of bugs related to the Android lifecycle &#8211; i.e. when Android decides to kill an old activity of yours.<\/p>\n<p><strong>Case Study:<\/strong><\/p>\n<p>User complaint: http:\/\/forum.videolan.org\/viewtopic.php?f=35&amp;t=108743<\/p>\n<p>The story so far&#8230; old versions of the VLC for Android beta, up to 0.0.10, suffer from a bug, where trying to delete something in another fragment causes the other (non-visible) fragment to receive the onContextItemSelected() call. After setting user visibility hints for that bug, a series of fragment related bugs appeared, mostly involving IllegalStateException in <code>android.support.v4.app.FragmentManager<\/code> because of duplicate fragments being added.<\/p>\n<p>The problems were caused when Android decided to kill unused background activities. The interface code would try to recreate and re-add() fragments, even though Android had automatically recreated them via &#8220;android.support.fragments&#8221; in the onCreate() bundle. This would manifest after opening another activity, shutting the screen off, or having a system popup appear.<\/p>\n<p>The solution was to keep better track of currently-added fragments, as member variables of the main activity may not necessarily be kept after the activity was hidden. To solve the problem, a check was implemented in onRestoreFragments() to &#8216;recall&#8217; fragments that were already added. This fixed the problem permanently.<\/p>\n<p><strong>The moral of the story:<\/strong> watch and manage fragments very carefully in order to prevent problems.<\/p>","protected":false},"excerpt":{"rendered":"<p>How to test a fragment-based Android application: Switching fragments directly from a sub-fragment (inside backstack) &#8211; This tests how well your fragment switching code deals with backstacks while switching fragments. Switching from another top level fragment &#8211; This tests your fragment switching code. Turning off the screen &#8211; This tests your onResume() and onResumeFragments() handlers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[759],"tags":[760,824,825],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/344"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}