android: fix Fragment be created twice

This commit is contained in:
pengfei.zhou 2022-03-08 17:13:48 +08:00 committed by osborn
parent e8e344ed21
commit 774d3160a9
4 changed files with 32 additions and 8 deletions

View File

@ -143,6 +143,9 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator {
AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() { AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() {
@Override @Override
public Object call() { public Object call() {
if (maskView == null) {
return null;
}
maskView.setVisibility(View.GONE); maskView.setVisibility(View.GONE);
return null; return null;
} }
@ -153,9 +156,18 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator {
AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() { AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() {
@Override @Override
public Object call() { public Object call() {
if (maskView == null) {
return null;
}
maskView.setVisibility(View.VISIBLE); maskView.setVisibility(View.VISIBLE);
maskView.findViewById(R.id.doric_mask_loading).setVisibility(View.VISIBLE); View loading = maskView.findViewById(R.id.doric_mask_loading);
maskView.findViewById(R.id.doric_mask_error).setVisibility(View.GONE); View error = maskView.findViewById(R.id.doric_mask_error);
if (loading != null) {
loading.setVisibility(View.VISIBLE);
}
if (error != null) {
error.setVisibility(View.GONE);
}
return null; return null;
} }
}); });
@ -165,9 +177,18 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator {
AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() { AsyncCall.ensureRunInHandler(uiHandler, new Callable<Object>() {
@Override @Override
public Object call() { public Object call() {
if (maskView == null) {
return null;
}
View loading = maskView.findViewById(R.id.doric_mask_loading);
View error = maskView.findViewById(R.id.doric_mask_error);
maskView.setVisibility(View.VISIBLE); maskView.setVisibility(View.VISIBLE);
maskView.findViewById(R.id.doric_mask_loading).setVisibility(View.GONE); if (loading != null) {
maskView.findViewById(R.id.doric_mask_error).setVisibility(View.VISIBLE); loading.setVisibility(View.GONE);
}
if (error != null) {
error.setVisibility(View.VISIBLE);
}
View retryView = maskView.findViewById(R.id.doric_mask_error_retry); View retryView = maskView.findViewById(R.id.doric_mask_error_retry);
if (retryView != null) { if (retryView != null) {
retryView.setOnClickListener(new View.OnClickListener() { retryView.setOnClickListener(new View.OnClickListener() {

View File

@ -9,8 +9,7 @@
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:defaultNavHost="true" app:defaultNavHost="true" />
app:navGraph="@navigation/doric_navigation" />
<FrameLayout <FrameLayout
android:id="@+id/doric_mask" android:id="@+id/doric_mask"

View File

@ -20,7 +20,9 @@ public class MainActivity extends AppCompatActivity {
getIntent().putExtra("alias", BUNDLE_NAME); getIntent().putExtra("alias", BUNDLE_NAME);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.getSupportFragmentManager().beginTransaction().add(R.id.container, new DoricFragment()).commit(); if (savedInstanceState == null) {
this.getSupportFragmentManager().beginTransaction().add(R.id.container, new DoricFragment()).commit();
}
BaseDoricNavBar doricNavBar = findViewById(R.id.doric_nav_bar); BaseDoricNavBar doricNavBar = findViewById(R.id.doric_nav_bar);
doricNavBar.setBackIconVisible(false); doricNavBar.setBackIconVisible(false);

View File

@ -20,7 +20,9 @@ public class MainActivity extends AppCompatActivity {
getIntent().putExtra("alias", BUNDLE_NAME); getIntent().putExtra("alias", BUNDLE_NAME);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.getSupportFragmentManager().beginTransaction().add(R.id.container, new DoricFragment()).commit(); if (savedInstanceState == null) {
this.getSupportFragmentManager().beginTransaction().add(R.id.container, new DoricFragment()).commit();
}
BaseDoricNavBar doricNavBar = findViewById(R.id.doric_nav_bar); BaseDoricNavBar doricNavBar = findViewById(R.id.doric_nav_bar);
doricNavBar.setBackIconVisible(false); doricNavBar.setBackIconVisible(false);