@@ -34,6 +34,7 @@ const ownerSymbol = Symbol("_owner");
3434let TabFragment : any ;
3535let BottomNavigationBar : any ;
3636let AttachStateChangeListener : any ;
37+ let appResources : android . content . res . Resources ;
3738
3839function makeFragmentName ( viewId : number , id : number ) : string {
3940 return "android:bottomnavigation:" + viewId + ":" + id ;
@@ -55,8 +56,9 @@ function initializeNativeClasses() {
5556 }
5657
5758 class TabFragmentImplementation extends org . nativescript . widgets . FragmentBase {
58- private tab : BottomNavigation ;
59+ private owner : BottomNavigation ;
5960 private index : number ;
61+ private backgroundBitmap : android . graphics . Bitmap = null ;
6062
6163 constructor ( ) {
6264 super ( ) ;
@@ -77,18 +79,61 @@ function initializeNativeClasses() {
7779 public onCreate ( savedInstanceState : android . os . Bundle ) : void {
7880 super . onCreate ( savedInstanceState ) ;
7981 const args = this . getArguments ( ) ;
80- this . tab = getTabById ( args . getInt ( TABID ) ) ;
82+ this . owner = getTabById ( args . getInt ( TABID ) ) ;
8183 this . index = args . getInt ( INDEX ) ;
82- if ( ! this . tab ) {
84+ if ( ! this . owner ) {
8385 throw new Error ( `Cannot find BottomNavigation` ) ;
8486 }
8587 }
8688
8789 public onCreateView ( inflater : android . view . LayoutInflater , container : android . view . ViewGroup , savedInstanceState : android . os . Bundle ) : android . view . View {
88- const tabItem = this . tab . items [ this . index ] ;
90+ const tabItem = this . owner . items [ this . index ] ;
8991
9092 return tabItem . nativeViewProtected ;
9193 }
94+
95+ public onDestroyView ( ) {
96+ const hasRemovingParent = this . getRemovingParentFragment ( ) ;
97+
98+ // Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments.
99+ // TODO: Consider removing it when update to androidx.fragment:1.2.0
100+ if ( hasRemovingParent && this . owner . selectedIndex === this . index ) {
101+ const bitmapDrawable = new android . graphics . drawable . BitmapDrawable ( appResources , this . backgroundBitmap ) ;
102+ this . owner . _originalBackground = this . owner . backgroundColor || new Color ( "White" ) ;
103+ this . owner . nativeViewProtected . setBackgroundDrawable ( bitmapDrawable ) ;
104+ this . backgroundBitmap = null ;
105+ }
106+
107+ super . onDestroyView ( ) ;
108+ }
109+
110+ public onPause ( ) : void {
111+ const hasRemovingParent = this . getRemovingParentFragment ( ) ;
112+
113+ // Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments.
114+ // TODO: Consider removing it when update to androidx.fragment:1.2.0
115+ if ( hasRemovingParent && this . owner . selectedIndex === this . index ) {
116+ this . backgroundBitmap = this . loadBitmapFromView ( this . owner . nativeViewProtected ) ;
117+ }
118+
119+ super . onPause ( ) ;
120+ }
121+
122+ private loadBitmapFromView ( view : android . view . View ) : android . graphics . Bitmap {
123+ // Another way to get view bitmap. Test performance vs setDrawingCacheEnabled
124+ // const width = view.getWidth();
125+ // const height = view.getHeight();
126+ // const bitmap = android.graphics.Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888);
127+ // const canvas = new android.graphics.Canvas(bitmap);
128+ // view.layout(0, 0, width, height);
129+ // view.draw(canvas);
130+
131+ view . setDrawingCacheEnabled ( true ) ;
132+ const bitmap = android . graphics . Bitmap . createBitmap ( view . getDrawingCache ( ) ) ;
133+ view . setDrawingCacheEnabled ( false ) ;
134+
135+ return bitmap ;
136+ }
92137 }
93138
94139 class BottomNavigationBarImplementation extends org . nativescript . widgets . BottomNavigationBar {
@@ -168,6 +213,7 @@ function initializeNativeClasses() {
168213 TabFragment = TabFragmentImplementation ;
169214 BottomNavigationBar = BottomNavigationBarImplementation ;
170215 AttachStateChangeListener = new AttachListener ( ) ;
216+ appResources = application . android . context . getResources ( ) ;
171217}
172218
173219function setElevation ( bottomNavigationBar : org . nativescript . widgets . BottomNavigationBar ) {
@@ -196,6 +242,7 @@ export class BottomNavigation extends TabNavigationBase {
196242 private _currentFragment : androidx . fragment . app . Fragment ;
197243 private _currentTransaction : androidx . fragment . app . FragmentTransaction ;
198244 private _attachedToWindow = false ;
245+ public _originalBackground : any ;
199246
200247 constructor ( ) {
201248 super ( ) ;
@@ -320,6 +367,12 @@ export class BottomNavigation extends TabNavigationBase {
320367 public onLoaded ( ) : void {
321368 super . onLoaded ( ) ;
322369
370+ if ( this . _originalBackground ) {
371+ this . backgroundColor = null ;
372+ this . backgroundColor = this . _originalBackground ;
373+ this . _originalBackground = null ;
374+ }
375+
323376 if ( this . tabStrip ) {
324377 this . setTabStripItems ( this . tabStrip . items ) ;
325378 } else {
@@ -334,8 +387,14 @@ export class BottomNavigation extends TabNavigationBase {
334387
335388 _onAttachedToWindow ( ) : void {
336389 super . _onAttachedToWindow ( ) ;
337-
338390 this . _attachedToWindow = true ;
391+
392+ // _onAttachedToWindow called from OS again after it was detach
393+ // TODO: Consider testing and removing it when update to androidx.fragment:1.2.0
394+ if ( this . _manager && this . _manager . isDestroyed ( ) ) {
395+ return ;
396+ }
397+
339398 this . changeTab ( this . selectedIndex ) ;
340399 }
341400
0 commit comments