navigate safely to avoid race (#1596)
This commit is contained in:
parent
a3fb878f46
commit
c7d82fee37
|
|
@ -120,7 +120,9 @@ public class AppListFragment extends BaseFragment {
|
||||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
if (module == null) {
|
if (module == null) {
|
||||||
getNavController().navigate(AppListFragmentDirections.actionAppListFragmentToModulesFragment());
|
if (!safeNavigate(AppListFragmentDirections.actionAppListFragmentToModulesFragment())) {
|
||||||
|
safeNavigate(R.id.modules_nav);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,10 +134,13 @@ public class AppListFragment extends BaseFragment {
|
||||||
int moduleUserId = args.getModuleUserId();
|
int moduleUserId = args.getModuleUserId();
|
||||||
|
|
||||||
module = ModuleUtil.getInstance().getModule(modulePackageName, moduleUserId);
|
module = ModuleUtil.getInstance().getModule(modulePackageName, moduleUserId);
|
||||||
if (module == null)
|
if (module == null) {
|
||||||
getNavController().navigate(R.id.action_app_list_fragment_to_modules_fragment);
|
if (!safeNavigate(R.id.action_app_list_fragment_to_modules_fragment)) {
|
||||||
|
safeNavigate(R.id.modules_nav);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
backupLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument(),
|
backupLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument("application/gzip"),
|
||||||
uri -> {
|
uri -> {
|
||||||
if (uri == null) return;
|
if (uri == null) return;
|
||||||
runAsync(() -> {
|
runAsync(() -> {
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,12 @@ import android.os.Looper;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.IdRes;
|
||||||
import androidx.annotation.StringRes;
|
import androidx.annotation.StringRes;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.navigation.NavController;
|
import androidx.navigation.NavController;
|
||||||
|
import androidx.navigation.NavDirections;
|
||||||
import androidx.navigation.fragment.NavHostFragment;
|
import androidx.navigation.fragment.NavHostFragment;
|
||||||
|
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||||
|
|
@ -52,6 +54,24 @@ public class BaseFragment extends Fragment {
|
||||||
return NavHostFragment.findNavController(this);
|
return NavHostFragment.findNavController(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean safeNavigate(@IdRes int resId) {
|
||||||
|
try {
|
||||||
|
getNavController().navigate(resId);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean safeNavigate(NavDirections direction) {
|
||||||
|
try {
|
||||||
|
getNavController().navigate(direction);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setupToolbar(Toolbar toolbar, View tipsView, int title) {
|
public void setupToolbar(Toolbar toolbar, View tipsView, int title) {
|
||||||
setupToolbar(toolbar, tipsView, getString(title), -1);
|
setupToolbar(toolbar, tipsView, getString(title), -1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -593,7 +593,7 @@ public class ModulesFragment extends BaseFragment implements ModuleUtil.ModuleLi
|
||||||
holder.root.setAlpha(moduleUtil.isModuleEnabled(item.packageName) ? 1.0f : .5f);
|
holder.root.setAlpha(moduleUtil.isModuleEnabled(item.packageName) ? 1.0f : .5f);
|
||||||
holder.itemView.setOnClickListener(v -> {
|
holder.itemView.setOnClickListener(v -> {
|
||||||
searchView.clearFocus();
|
searchView.clearFocus();
|
||||||
getNavController().navigate(ModulesFragmentDirections.actionModulesFragmentToAppListFragment(item.packageName, item.userId));
|
safeNavigate(ModulesFragmentDirections.actionModulesFragmentToAppListFragment(item.packageName, item.userId));
|
||||||
});
|
});
|
||||||
holder.itemView.setOnLongClickListener(v -> {
|
holder.itemView.setOnLongClickListener(v -> {
|
||||||
searchView.clearFocus();
|
searchView.clearFocus();
|
||||||
|
|
|
||||||
|
|
@ -316,7 +316,7 @@ public class RepoFragment extends BaseFragment implements RepoLoader.RepoListene
|
||||||
|
|
||||||
holder.itemView.setOnClickListener(v -> {
|
holder.itemView.setOnClickListener(v -> {
|
||||||
searchView.clearFocus();
|
searchView.clearFocus();
|
||||||
getNavController().navigate(RepoFragmentDirections.actionRepoFragmentToRepoItemFragment(module.getName()));
|
safeNavigate(RepoFragmentDirections.actionRepoFragmentToRepoItemFragment(module.getName()));
|
||||||
});
|
});
|
||||||
holder.itemView.setTooltipText(module.getDescription());
|
holder.itemView.setTooltipText(module.getDescription());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -134,8 +134,11 @@ public class RepoItemFragment extends BaseFragment implements RepoLoader.RepoLis
|
||||||
|
|
||||||
String modulePackageName = getArguments() == null ? null : getArguments().getString("modulePackageName");
|
String modulePackageName = getArguments() == null ? null : getArguments().getString("modulePackageName");
|
||||||
module = RepoLoader.getInstance().getOnlineModule(modulePackageName);
|
module = RepoLoader.getInstance().getOnlineModule(modulePackageName);
|
||||||
if (module == null)
|
if (module == null) {
|
||||||
getNavController().navigate(R.id.action_repo_item_fragment_to_repo_fragment);
|
if (!safeNavigate(R.id.action_repo_item_fragment_to_repo_fragment)) {
|
||||||
|
safeNavigate(R.id.repo_nav);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderGithubMarkdown(WebView view, String text) {
|
private void renderGithubMarkdown(WebView view, String text) {
|
||||||
|
|
@ -561,7 +564,9 @@ public class RepoItemFragment extends BaseFragment implements RepoLoader.RepoLis
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||||
var parent = getParentFragment();
|
var parent = getParentFragment();
|
||||||
if (!(parent instanceof RepoItemFragment)) {
|
if (!(parent instanceof RepoItemFragment)) {
|
||||||
getNavController().navigate(R.id.action_repo_item_fragment_to_repo_fragment);
|
if (!safeNavigate(R.id.action_repo_item_fragment_to_repo_fragment)) {
|
||||||
|
safeNavigate(R.id.repo_nav);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var repoItemFragment = (RepoItemFragment) parent;
|
var repoItemFragment = (RepoItemFragment) parent;
|
||||||
|
|
@ -590,7 +595,9 @@ public class RepoItemFragment extends BaseFragment implements RepoLoader.RepoLis
|
||||||
var arguments = getArguments();
|
var arguments = getArguments();
|
||||||
var parent = getParentFragment();
|
var parent = getParentFragment();
|
||||||
if (arguments == null || !(parent instanceof RepoItemFragment)) {
|
if (arguments == null || !(parent instanceof RepoItemFragment)) {
|
||||||
getNavController().navigate(R.id.action_repo_item_fragment_to_repo_fragment);
|
if (!safeNavigate(R.id.action_repo_item_fragment_to_repo_fragment)) {
|
||||||
|
safeNavigate(R.id.repo_nav);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var repoItemFragment = (RepoItemFragment) parent;
|
var repoItemFragment = (RepoItemFragment) parent;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue