Skip to content

Commit

Permalink
Merge pull request #112 from adolgiy/hotfix/fragment-factory-npe
Browse files Browse the repository at this point in the history
Fix NPE when localStackCopy.size() == 0 and FragmentFactory is used
  • Loading branch information
terrakok committed May 10, 2020
2 parents dcf1532 + 05a8ec8 commit 7c31cdc
Showing 1 changed file with 58 additions and 44 deletions.
Expand Up @@ -3,17 +3,24 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.terrakok.cicerone.Navigator;
import ru.terrakok.cicerone.commands.*;

import java.util.LinkedList;

import ru.terrakok.cicerone.Navigator;
import ru.terrakok.cicerone.commands.Back;
import ru.terrakok.cicerone.commands.BackTo;
import ru.terrakok.cicerone.commands.Command;
import ru.terrakok.cicerone.commands.Forward;
import ru.terrakok.cicerone.commands.Replace;

/**
* Navigator implementation for launch fragments and activities.<br>
* Feature {@link BackTo} works only for fragments.<br>
Expand Down Expand Up @@ -98,24 +105,7 @@ protected void fragmentForward(@NotNull Forward command) {
FragmentParams fragmentParams = screen.getFragmentParams();
Fragment fragment = fragmentParams == null ? createFragment(screen) : null;

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

setupFragmentTransaction(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction);

if (fragmentParams != null) {
fragmentTransaction.replace(containerId, fragmentParams.getFragmentClass(), fragmentParams.getArguments());
} else {
fragmentTransaction.replace(containerId, fragment);
}

fragmentTransaction
.addToBackStack(screen.getScreenKey())
.commit();
localStackCopy.add(screen.getScreenKey());
forwardFragmentInternal(command, screen, fragmentParams, fragment);
}

protected void fragmentBack() {
Expand Down Expand Up @@ -155,6 +145,9 @@ protected void fragmentReplace(@NotNull Replace command) {
fragmentManager.popBackStack();
localStackCopy.removeLast();

forwardFragmentInternal(command, screen, fragmentParams, fragment);

} else {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

setupFragmentTransaction(
Expand All @@ -164,32 +157,53 @@ protected void fragmentReplace(@NotNull Replace command) {
fragmentTransaction
);

if (fragmentParams != null) {
fragmentTransaction.replace(
containerId,
fragmentParams.getFragmentClass(),
fragmentParams.getArguments());
} else {
fragmentTransaction.replace(containerId, fragment);
}
fragmentTransaction
.addToBackStack(screen.getScreenKey())
.commit();
localStackCopy.add(screen.getScreenKey());
replaceFragmentInternal(fragmentTransaction, screen, fragmentParams, fragment);

} else {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.commit();
}
}

setupFragmentTransaction(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);
private void forwardFragmentInternal(
@NotNull Command command,
@NotNull SupportAppScreen screen,
@Nullable FragmentParams fragmentParams,
@Nullable Fragment fragment
) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

setupFragmentTransaction(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

replaceFragmentInternal(fragmentTransaction, screen, fragmentParams, fragment);

fragmentTransaction
.addToBackStack(screen.getScreenKey())
.commit();

fragmentTransaction
.replace(containerId, fragment)
.commit();
localStackCopy.add(screen.getScreenKey());
}

private void replaceFragmentInternal(
@NotNull FragmentTransaction transaction,
@NotNull SupportAppScreen screen,
@Nullable FragmentParams params,
@Nullable Fragment fragment
) {
if (params != null) {
transaction.replace(
containerId,
params.getFragmentClass(),
params.getArguments()
);
} else if (fragment != null) {
transaction.replace(containerId, fragment);
} else {
throw new IllegalArgumentException("Either 'params' or 'fragment' shouldn't " +
"be null for " + screen.getScreenKey());
}
}

Expand Down Expand Up @@ -307,7 +321,7 @@ protected void errorWhileCreatingScreen(@NotNull SupportAppScreen screen) {
* Override this method if you want to handle apply command error.
*
* @param command command
* @param error error
* @param error error
*/
protected void errorOnApplyCommand(
@NotNull Command command,
Expand Down

0 comments on commit 7c31cdc

Please sign in to comment.