Repository: NativeScript/NativeScript
File: packages/core/ui/frame/index.android.ts (TypeScript)

Code before:
			}
		}, 0);

		super.onLoaded();
	}

	onUnloaded() {
		super.onUnloaded();
		if (typeof this._frameCreateTimeout === 'number') {
			clearTimeout(this._frameCreateTimeout);
			this._frameCreateTimeout = null;
		}
	}

	private disposeCurrentFragment(): void {
		if (!this._currentEntry || !this._currentEntry.fragment || !this._currentEntry.fragment.isAdded()) {
			return;
		}
		const fragment: androidx.fragment.app.Fragment = this._currentEntry.fragment;
		const fragmentManager: androidx.fragment.app.FragmentManager = fragment.getFragmentManager();

		const transaction = fragmentManager.beginTransaction();
		const fragmentExitTransition = fragment.getExitTransition();

		// Reset animation to its initial state to prevent mirrored effect when restore current fragment transitions
		if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) {
			fragmentExitTransition.setResetOnTransitionEnd(true);
		}

		transaction.remove(fragment);
		transaction.commitNowAllowingStateLoss();
	}

	private createFragment(backstackEntry: BackstackEntry, fragmentTag: string): androidx.fragment.app.Fragment {
		ensureFragmentClass();
		const newFragment = new fragmentClass();
		const args = new android.os.Bundle();
		args.putInt(FRAMEID, this._android.frameId);
		newFragment.setArguments(args);
		setFragmentCallbacks(newFragment);

		const callbacks = newFragment[CALLBACKS];
		callbacks.frame = this;
		callbacks.entry = backstackEntry;

		// backstackEntry
		backstackEntry.fragment = newFragment;
		backstackEntry.fragmentTag = fragmentTag;
		backstackEntry.navDepth = navDepth;

		return newFragment;

Review comment (bug):
@NathanWalker @edusperoni I kept this one in hope of still using it as a precaution but it might no longer be needed. I wonder if there's still a sample we could retry the nested frame bug that the check was intended for.
```
// there's a bug with nested frames where sometimes the nested fragment is not recreated at all
// so we manually check on loaded event if the fragment is not recreated and recreate it
```

Code after:
		super.onLoaded();
	}

	onUnloaded() {
		super.onUnloaded();

		if (typeof this._frameCreateTimeout === 'number') {
			clearTimeout(this._frameCreateTimeout);
			this._frameCreateTimeout = null;
		}
	}

	/**
	 * TODO: Check if this fragment precaution is still needed
	 */
	private _ensureEntryFragment(): void {
		// in case the activity is "reset" using resetRootView or disposed we must wait for
		// the attachedToWindow event to make the first navigation or it will crash
		// https://github.com/NativeScript/NativeScript/commit/9dd3e1a8076e5022e411f2f2eeba34aabc68d112
		// though we should not do it on app "start"
		// or it will create a "flash" to activity background color
		if (this._isReset && !this._attachedToWindow) {
			return;
		}

		this._frameCreateTimeout = setTimeout(() => {
			// there's a bug with nested frames where sometimes the nested fragment is not recreated at all
			// so we manually check on loaded event if the fragment is not recreated and recreate it
			const currentEntry = this._currentEntry || this._executingContext?.entry;
			if (currentEntry) {
				if (!currentEntry.fragment) {
					const manager = this._getFragmentManager();
					const transaction = manager.beginTransaction();
					currentEntry.fragment = this.createFragment(currentEntry, currentEntry.fragmentTag);
					_updateTransitions(currentEntry);
					transaction.replace(this.containerViewId, currentEntry.fragment, currentEntry.fragmentTag);
					transaction.commitAllowingStateLoss();
				}
			}

			this._frameCreateTimeout = null;
		}, 0);
	}

	private disposeCurrentFragment(): void {
		if (!this._currentEntry || !this._currentEntry.fragment || !this._currentEntry.fragment.isAdded()) {
			return;
		}
		const fragment: androidx.fragment.app.Fragment = this._currentEntry.fragment;
		const fragmentManager: androidx.fragment.app.FragmentManager = fragment.getFragmentManager();