Skip to content

Commit

Permalink
fix(android): lollipop crash on changing activity context after navig…
Browse files Browse the repository at this point in the history
…ation w/transition (#5700)
  • Loading branch information
manoldonev authored Apr 20, 2018
1 parent 1536d15 commit 4f5887b
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions tns-core-modules/ui/frame/frame.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { profile } from "../../profiling";
// TODO: Remove this and get it from global to decouple builder for angular
import { createViewFromEntry } from "../builder";

import { device } from "../../platform";
import lazy from "../../utils/lazy";

export * from "./frame-common";

const INTENT_EXTRA = "com.tns.activity";
Expand All @@ -32,6 +35,8 @@ const CALLBACKS = "_callbacks";
const ownerSymbol = Symbol("_owner");
const activityRootViewsMap = new Map<number, WeakRef<View>>();

const sdkVersion = lazy(() => parseInt(device.sdkVersion));

let navDepth = -1;
let fragmentId = -1;
export let moduleLoaded: boolean;
Expand Down Expand Up @@ -186,13 +191,28 @@ export class Frame extends FrameBase {
super.onUnloaded();
}

private disposeCurrentFragment(){
if (this._currentEntry && this._currentEntry.fragment) {
const manager: android.app.FragmentManager = this._getFragmentManager();
const transaction = manager.beginTransaction();
private disposeCurrentFragment(): void {
if (!this._currentEntry || !this._currentEntry.fragment) {
return;
}

const manager: android.app.FragmentManager = this._getFragmentManager();
const transaction = manager.beginTransaction();
const androidSdkVersion = sdkVersion();

if (androidSdkVersion !== 21 && androidSdkVersion !== 22) {
transaction.remove(this._currentEntry.fragment);
transaction.commitAllowingStateLoss();
} else {
// https://github.com/NativeScript/NativeScript/issues/5674
// HACK: Add and remove dummy fragment to workaround a Lollipop issue
// with inFragment passed as null when adding transition targets: https://android.googlesource.com/platform/frameworks/base.git/+/lollipop-release/core/java/android/app/BackStackRecord.java#1127
const dummyFragmentTag = "dummy";
const dummyFragment = this.createFragment(<BackstackEntry>{}, dummyFragmentTag);
transaction.replace(this.containerViewId, dummyFragment, dummyFragmentTag);
transaction.remove(dummyFragment);
}

transaction.commitAllowingStateLoss();
}

private createFragment(backstackEntry: BackstackEntry, fragmentTag: string): android.app.Fragment {
Expand Down

0 comments on commit 4f5887b

Please sign in to comment.