Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 1    Views: 352

Veteran developer
Posts: 111
Reg: Oct 19, 2013
Fourm StarFourm Star
08/24/18 04:03 AM (5 years ago)

Android fragment stack... problem & solution (to whom it may concern...)

In the last days I tried to understand some strange effects of my Android source code, mainly the "handleBackButton" functions that should be processed after you press the back button on a specific screen. What is the problem? Whenever you put your own source code in a plugin code by a structure like @Override public void handleBackButton() { BT_debugger.showIt(fragmentName + ":handleBackButton"); (... your code... e. g., save some data...) } this code should be called from the main activity (BT_activity_host) within the "onBackPressed" function. However, when I set a break point in my handleBackButton()... it sometimes happened that this code was never executed. Sometimes it was executed twice... Now I found the solution which is relevant if you put several fragments onto the "backStack" of the activity, e. g. if you open a screen (first fragment on the stack), then you click the right navigation button and open the next screen (second fragment on the stack). BT_activity_host updates the variable "currentFragment" to the second fragment and when you click the "back button" it performs handleBackButton() of this second fragment. However, it does not update the "currentFragment" in BT_activity_host back to the first fragment. Instead, if you click the "back button" a second time, it performs handleBackButton() of the second fragment a second time, but does not perform the handleBackButton() code of the first fragment. Sounds complicated? It is... and if you never launch more than one fragment from the start screen, you can ignore this completely. However, if you launch more than one fragment (e.g. by using right navigation buttons) you may be happy to include the following code in BT_activity_host.java: 1. Look for the following code in BT_activity_host.java in onBackPressed(): ****************************************************************** //fire the handleBackButton method if we found it... if (backMethod != null) { try { backMethod.invoke(currentFragment); } catch (Exception e) { BT_debugger.showIt(activityName + ":onBackPressed. EXCEPTION (1): " + e.toString()); } } } ****************************************************************** 2. Below these lines add the following lines //------------------------------ // update currentFragment //------------------------------ FragmentManager fm = getSupportFragmentManager(); List<Fragment> myFragments = fm.getFragments(); if (myFragments.size() > 0) { currentFragment = myFragments.get(myFragments.size()-1); } Happy coding Thomas
Smugger than thou...
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
Fourm StarFourm StarFourm Star
08/26/18 02:30 AM (5 years ago)
Thank you Thomas!! This goes into my collection! Cheers! -- Smug

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.