我已經在應用中創建了一個對話框片段。以下是它的代碼。Android對話框片段不解除
public class AlertLoader extends DialogFragment {
Typeface fontRegular;
Bundle bundle;
String displayText = "";
public static AlertLoader newInstance(@NonNull String displayText) {
AlertLoader alertPopUpMenu = new AlertLoader();
Bundle args = new Bundle();
args.putString("displayText", displayText);
alertPopUpMenu.setArguments(args);
return alertPopUpMenu;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null && dialog.getWindow() != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = getArguments();
setStyle(STYLE_NO_TITLE, R.style.LoaderDialogTheme);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setCanceledOnTouchOutside(false);
return inflater.inflate(R.layout.alert_dialog_loader, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);
}
private void init(View mainView) {
displayText = bundle.getString("displayText");
TextView mLoadingText = (TextView) mainView.findViewById(R.id.loadertext);
mLoadingText.setTextColor(Color.WHITE);
mLoadingText.setTypeface(fontRegular);
if (displayText == null || displayText.length() == 0) {
mLoadingText.setVisibility(View.GONE);
} else {
mLoadingText.setText(displayText);
mLoadingText.setVisibility(View.VISIBLE);
}
}
public void dismissCurrentView() {
getDialog().dismiss();
}
}
我正在使用下面的代碼來顯示對話框。
private void showLoader(String displayText) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
AlertLoader newFragment = AlertLoader.newInstance(displayText);
newFragment.show(ft, "dialog");
}
它工作正常。我試圖通過下面的代碼解僱它。
private void dismissLoader() {
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
DialogFragment df = (DialogFragment) prev;
df.dismiss();
}
}
我在導航到下一個活動之前解僱了我的裝載程序。以下是我的代碼,我正在解僱裝載機。
dismissLoader();
Intent intent = new Intent(getActivity(), ShareMediaActivity.class);
intent.putExtra(UserDefault.bundlePath, file.getAbsolutePath());
startActivity(intent);
但是,當我回到以前的活動,對話框仍然顯示。在其他情況下,用於顯示和解僱的代碼工作正常。
我已經嘗試了幾種方法來解僱它,但不工作。任何人都可以幫助我呢?
感謝。
沒有你的代碼落在這條線之下((前!= NULL))創建一個域? – k0sh
在'AlertLoader'中投射'prev'然後嘗試... –
@ k0sh是的。 – Beena