mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 02:54:11 +08:00
feat(android): fragment transactions to use 'add' instead of 'replace' on fwd navigation (#8791)
Changes the behavior of android fragment transactions to use `add` instead of `replace` on forward navigation. BREAKING CHANGE: Changes the internal behavior of Android navigation: * while navigating forward, the page navigated from is not unloaded anymore * events order is changed in the sense that now `unloaded` happens after `navigatedFrom` instead of before There are multiple plus sides to this: * no more black views on navigation when using opengl (maps, ...) * navigation is faster, especially the navigation back! No longer need to recreate the page anymore. Navigation forward also gets faster as we no longer unload the previous page * navigatedFrom event happens faster * this the default behavior used by most of the android native apps
This commit is contained in:

committed by
Nathan Walker

parent
1e3bc48737
commit
e17e46974b
@ -181,7 +181,7 @@ function _test_PageNavigation_EventSequence(withTransition: boolean) {
|
|||||||
helper.navigateWithEntry(navigationEntry);
|
helper.navigateWithEntry(navigationEntry);
|
||||||
helper.goBack();
|
helper.goBack();
|
||||||
|
|
||||||
const expectedEventSequence = ['navigatingTo', 'loaded', 'navigatedTo', 'navigatingFrom', 'unloaded', 'navigatedFrom'];
|
const expectedEventSequence = ['navigatingTo', 'loaded', 'navigatedTo', 'navigatingFrom', 'navigatedFrom', 'unloaded'];
|
||||||
TKUnit.arrayAssert(eventSequence, expectedEventSequence, 'Actual event sequence is not equal to expected. Actual: ' + eventSequence + '; Expected: ' + expectedEventSequence);
|
TKUnit.arrayAssert(eventSequence, expectedEventSequence, 'Actual event sequence is not equal to expected. Actual: ' + eventSequence + '; Expected: ' + expectedEventSequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ let AnimationListener: android.animation.Animator.AnimatorListener;
|
|||||||
|
|
||||||
interface ExpandedTransitionListener extends androidx.transition.Transition.TransitionListener {
|
interface ExpandedTransitionListener extends androidx.transition.Transition.TransitionListener {
|
||||||
entry: ExpandedEntry;
|
entry: ExpandedEntry;
|
||||||
|
backEntry?: BackstackEntry;
|
||||||
transition: androidx.transition.Transition;
|
transition: androidx.transition.Transition;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +144,7 @@ export function _setAndroidFragmentTransitions(animated: boolean, navigationTran
|
|||||||
if (currentFragmentNeedsDifferentAnimation) {
|
if (currentFragmentNeedsDifferentAnimation) {
|
||||||
setupCurrentFragmentFadeTransition(navigationTransition, currentEntry);
|
setupCurrentFragmentFadeTransition(navigationTransition, currentEntry);
|
||||||
}
|
}
|
||||||
} else if (name === 'explode') {
|
} else if (name === 'explode') {
|
||||||
setupNewFragmentExplodeTransition(navigationTransition, newEntry);
|
setupNewFragmentExplodeTransition(navigationTransition, newEntry);
|
||||||
if (currentFragmentNeedsDifferentAnimation) {
|
if (currentFragmentNeedsDifferentAnimation) {
|
||||||
setupCurrentFragmentExplodeTransition(navigationTransition, currentEntry);
|
setupCurrentFragmentExplodeTransition(navigationTransition, currentEntry);
|
||||||
@ -223,6 +224,7 @@ function getAnimationListener(): android.animation.Animator.AnimatorListener {
|
|||||||
|
|
||||||
onAnimationStart(animator: ExpandedAnimator): void {
|
onAnimationStart(animator: ExpandedAnimator): void {
|
||||||
const entry = animator.entry;
|
const entry = animator.entry;
|
||||||
|
const backEntry = animator.backEntry;
|
||||||
addToWaitingQueue(entry);
|
addToWaitingQueue(entry);
|
||||||
if (Trace.isEnabled()) {
|
if (Trace.isEnabled()) {
|
||||||
Trace.write(`START ${animator.transitionType} for ${entry.fragmentTag}`, Trace.categories.Transition);
|
Trace.write(`START ${animator.transitionType} for ${entry.fragmentTag}`, Trace.categories.Transition);
|
||||||
@ -236,10 +238,13 @@ function getAnimationListener(): android.animation.Animator.AnimatorListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onAnimationEnd(animator: ExpandedAnimator): void {
|
onAnimationEnd(animator: ExpandedAnimator): void {
|
||||||
|
const entry = animator.entry;
|
||||||
|
const backEntry = animator.backEntry;
|
||||||
if (Trace.isEnabled()) {
|
if (Trace.isEnabled()) {
|
||||||
Trace.write(`END ${animator.transitionType} for ${animator.entry.fragmentTag}`, Trace.categories.Transition);
|
Trace.write(`END ${animator.transitionType} for ${entry.fragmentTag} backEntry:${backEntry ? backEntry.fragmentTag : 'none'}`, Trace.categories.Transition);
|
||||||
}
|
}
|
||||||
transitionOrAnimationCompleted(animator.entry, animator.backEntry);
|
transitionOrAnimationCompleted(entry, backEntry);
|
||||||
|
animator.backEntry = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
onAnimationCancel(animator: ExpandedAnimator): void {
|
onAnimationCancel(animator: ExpandedAnimator): void {
|
||||||
@ -345,10 +350,12 @@ function getTransitionListener(entry: ExpandedEntry, transition: androidx.transi
|
|||||||
|
|
||||||
onTransitionEnd(transition: androidx.transition.Transition): void {
|
onTransitionEnd(transition: androidx.transition.Transition): void {
|
||||||
const entry = this.entry;
|
const entry = this.entry;
|
||||||
|
const backEntry = this.backEntry;
|
||||||
if (Trace.isEnabled()) {
|
if (Trace.isEnabled()) {
|
||||||
Trace.write(`END ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
|
Trace.write(`END ${toShortString(transition)} transition for ${entry.fragmentTag} backEntry:${backEntry ? backEntry.fragmentTag : 'none'}`, Trace.categories.Transition);
|
||||||
}
|
}
|
||||||
transitionOrAnimationCompleted(entry, this.backEntry);
|
transitionOrAnimationCompleted(entry, backEntry);
|
||||||
|
this.backEntry = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
onTransitionResume(transition: androidx.transition.Transition): void {
|
onTransitionResume(transition: androidx.transition.Transition): void {
|
||||||
@ -660,6 +667,7 @@ function transitionOrAnimationCompleted(entry: ExpandedEntry, backEntry: Backsta
|
|||||||
if (!entries) {
|
if (!entries) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
console.log('transitionOrAnimationCompleted', frameId, backEntry && backEntry.fragmentTag, waitingQueue.size, entries.size, completedEntries.size );
|
||||||
|
|
||||||
entries.delete(entry);
|
entries.delete(entry);
|
||||||
if (entries.size === 0) {
|
if (entries.size === 0) {
|
||||||
|
@ -266,8 +266,8 @@ export class FrameBase extends CustomLayoutView {
|
|||||||
public _updateBackstack(entry: BackstackEntry, navigationType: NavigationType): void {
|
public _updateBackstack(entry: BackstackEntry, navigationType: NavigationType): void {
|
||||||
const isBack = navigationType === NavigationType.back;
|
const isBack = navigationType === NavigationType.back;
|
||||||
const isReplace = navigationType === NavigationType.replace;
|
const isReplace = navigationType === NavigationType.replace;
|
||||||
this.raiseCurrentPageNavigatedEvents(isBack);
|
|
||||||
const current = this._currentEntry;
|
const current = this._currentEntry;
|
||||||
|
this.raiseCurrentPageNavigatedEvents(isBack);
|
||||||
|
|
||||||
// Do nothing for Hot Module Replacement
|
// Do nothing for Hot Module Replacement
|
||||||
if (isBack) {
|
if (isBack) {
|
||||||
@ -303,10 +303,6 @@ export class FrameBase extends CustomLayoutView {
|
|||||||
private raiseCurrentPageNavigatedEvents(isBack: boolean) {
|
private raiseCurrentPageNavigatedEvents(isBack: boolean) {
|
||||||
const page = this.currentPage;
|
const page = this.currentPage;
|
||||||
if (page) {
|
if (page) {
|
||||||
if (page.isLoaded) {
|
|
||||||
// Forward navigation does not remove page from frame so we raise unloaded manually.
|
|
||||||
page.callUnloaded();
|
|
||||||
}
|
|
||||||
page.onNavigatedFrom(isBack);
|
page.onNavigatedFrom(isBack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import { Builder } from '../builder';
|
|||||||
import { CSSUtils } from '../../css/system-classes';
|
import { CSSUtils } from '../../css/system-classes';
|
||||||
import { Device } from '../../platform';
|
import { Device } from '../../platform';
|
||||||
import { profile } from '../../profiling';
|
import { profile } from '../../profiling';
|
||||||
|
import { ExpandedEntry } from './fragment.transitions.android';
|
||||||
|
|
||||||
export * from './frame-common';
|
export * from './frame-common';
|
||||||
|
|
||||||
@ -325,6 +326,8 @@ export class Frame extends FrameBase {
|
|||||||
|
|
||||||
// If we had real navigation process queue.
|
// If we had real navigation process queue.
|
||||||
this._processNavigationQueue(entry.resolvedPage);
|
this._processNavigationQueue(entry.resolvedPage);
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Otherwise currentPage was recreated so this wasn't real navigation.
|
// Otherwise currentPage was recreated so this wasn't real navigation.
|
||||||
// Continue with next item in the queue.
|
// Continue with next item in the queue.
|
||||||
@ -437,7 +440,14 @@ export class Frame extends FrameBase {
|
|||||||
//transaction.setTransition(androidx.fragment.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
//transaction.setTransition(androidx.fragment.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
transaction.replace(this.containerViewId, newFragment, newFragmentTag);
|
if (clearHistory || isReplace) {
|
||||||
|
transaction.replace(this.containerViewId, newFragment, newFragmentTag);
|
||||||
|
} else {
|
||||||
|
transaction.add(this.containerViewId, newFragment, newFragmentTag);
|
||||||
|
}
|
||||||
|
if (this._currentEntry && this._currentEntry.entry.backstackVisible === false) {
|
||||||
|
transaction.remove(this._currentEntry.fragment);
|
||||||
|
}
|
||||||
transaction.commitAllowingStateLoss();
|
transaction.commitAllowingStateLoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -459,8 +469,26 @@ export class Frame extends FrameBase {
|
|||||||
|
|
||||||
_reverseTransitions(backstackEntry, this._currentEntry);
|
_reverseTransitions(backstackEntry, this._currentEntry);
|
||||||
|
|
||||||
transaction.replace(this.containerViewId, backstackEntry.fragment, backstackEntry.fragmentTag);
|
const currentIndex =this.backStack.length;
|
||||||
|
const goBackToIndex = this.backStack.indexOf(backstackEntry);
|
||||||
|
|
||||||
|
// the order is important so that the transition listener called be
|
||||||
|
// the one from the current entry we are going back from
|
||||||
|
if (this._currentEntry !== backstackEntry) {
|
||||||
|
const entry = this._currentEntry as ExpandedEntry;
|
||||||
|
// if we are going back we need to store where we are backing to
|
||||||
|
// so that we can set the current entry
|
||||||
|
// it only needs to be done on the return transition
|
||||||
|
if (entry.returnTransitionListener) {
|
||||||
|
entry.returnTransitionListener.backEntry = backstackEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.remove((this._currentEntry).fragment);
|
||||||
|
}
|
||||||
|
for (let index = goBackToIndex + 1; index < currentIndex; index++) {
|
||||||
|
transaction.remove(this.backStack[index].fragment);
|
||||||
|
}
|
||||||
|
|
||||||
transaction.commitAllowingStateLoss();
|
transaction.commitAllowingStateLoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -752,6 +780,12 @@ function findPageForFragment(fragment: androidx.fragment.app.Fragment, frame: Fr
|
|||||||
entry = current;
|
entry = current;
|
||||||
} else if (executingContext && executingContext.entry && executingContext.entry.fragmentTag === fragmentTag) {
|
} else if (executingContext && executingContext.entry && executingContext.entry.fragmentTag === fragmentTag) {
|
||||||
entry = executingContext.entry;
|
entry = executingContext.entry;
|
||||||
|
} else {
|
||||||
|
frame.backStack.forEach(e=>{
|
||||||
|
if (e && e.fragmentTag === fragmentTag) {
|
||||||
|
entry = e;
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let page: Page;
|
let page: Page;
|
||||||
|
Reference in New Issue
Block a user