I have a dialog fragment with a simple indeterminate progress bar in the centre, which i use to show network activity:
public class NativeLoadingDialogFragment extends DialogFragment {
public NativeLoadingDialogFragment() {
// Blank
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
ProgressBar indeterminateProgressBar = new ProgressBar(getActivity());
indeterminateProgressBar.setIndeterminate(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(indeterminateProgressBar);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
}
public boolean isShowing() {
return getDialog() != null;
}
}
I have used the dialog fragment throughout my app with no issues, it shows up without issue in lots of places when i call dialog.show(getFragmentManager, null), however when I try to call it in onResume of my settings activity it does not show!
I have an activity for settings, which launches the system settings to change the language of the phone. Once the user changes the language and my activity resumes I detect if the language has changed and do a network call:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mLoading = new NativeLoadingDialogFragment();
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(EXTRA_LANGUAGE)) {
String language = savedInstanceState.getString(EXTRA_LANGUAGE);
String currentLanguage = AppUtils.getDefaultLanguageCode(
SmartBankConstant.DEFAULT_LANGUAGE,
SmartBankConstant.SUPPORTED_LANGUAGES);
if (!language.equals(currentLanguage)) {
updateLanguage(Language.stringToLanguage(currentLanguage));
}
}
}
}
private void updateLanguage(Language newLanguage) {
....
getSpiceManager().execute(new SetLanguageRequest(newLanguage),
new SetLanguageRequestListener(this));
mLoading.show(getFragmentManager(), null);
getFragmentManager().executePendingTransactions();
}
The code definitely runs but no dialog appears! If the network call fails I have a retry option that calls the updateLanguage(Language newLanguage) method again, and the dialog actually appears that time! What am I doing wrong?
Try with this approach. It checks if the dialog is already displayed, otherwise it shows it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mLoading = new NativeLoadingDialogFragment();
if (savedInstanceState != null) {
...
}
mLoading.show(getFragmentManager(), null);
}
private void updateLanguage(Language newLanguage) {
...
if (mLoading != null && !mLoading.isVisible()) {
mLoading.show(getFragmentManager(), null);
getFragmentManager().executePendingTransactions();
}
}
I don't know why, but running fragment transaction in the next loop helped to solve this issue.
new Handler().post(new Runnable() {
#Override public void run() {
// for some reason this must be called in the next loop
dialogFragment.show(getSupportFragmentManager(), tag);
}
});
Related
I am working on project, which simply validates through username and password.
I made some progress with using DialogFragments and AlertDialog. AlertDialog appears after starting the app over the mainactivity asking for username and password.
I must set the Alertdialog's setCanceledOnTouchOutside(false) and DialogFragment's setCancelable(false) because I don't want the users to dismiss it with pressing android's back button.
The problem is, after dismissing it programatically on successful login, if the activity becomes invisible and visible again , the Alertdialog's OnShowListener called, showing this AlertDialog again.
Can I somehow "detach" this AlertDialog from Activity? This popups also happen after unlocking the screen and getting back to activity which makes it very annoying...
Here is the code of interest:
MainActivity
public class MainActivity extends AppCompatActivity implements NoticeDialogFragment.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(GlobalInformations.getInstance().getUsername()==null){
shownoticeDialog();
}
}
public void shownoticeDialog(){
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
}
#Override
public void onDismiss(DialogFragment dialog) {
//set the username on a TextView instance, etc...
}
NoticeDialogFragment extends DialogFragment
public class NoticeDialogFragment extends DialogFragment {
public interface NoticeDialogListener{
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
public void onDismiss(DialogFragment dialog);
}
NoticeDialogListener mListener;
static Activity activity = null;
//static String username;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activity = (Activity) context;
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e){
throw new ClassCastException(activity.toString() + "must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
final AutoCompleteTextView actv_username = (AutoCompleteTextView) view.findViewById(R.id.username);
final EditText password = (EditText) view.findViewById(R.id.password);
getavailableusernames(actv_username);
final AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(getContext(), R.style.AlertDialogCustom))
.setView(view)
.setTitle("Login")
.setPositiveButton("OK", null)
//.setNegativeButton("Cancel", null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
final Button button =((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String passw = password.getText().toString();
String user = actv_username.getText().toString();
try{
if(user.length()<4 || passw.length()<4){
Toast.makeText(getContext(), "Username/password too short", Toast.LENGTH_SHORT).show();
dialog.show();
}
else {
//login to account, if success dismiss.
login(user, passw,dialog);
}
} catch(Exception e){
}
// dialog.dismiss();
}
});
}
});
dialog.setCanceledOnTouchOutside(false);
// set the DialogFragment to make the dialog unable to dismiss with back button
// (because not working if called on the dialog directly)
this.setCancelable(false);
return dialog;
}
public void login(final String username, String password, final AlertDialog dialog){
boolean login_success = false;
//query the credentials
login_success = dosomesqlquery(username, password);
if(login_success){
dialog.dismiss();
}
}
//passing the handling to activity...
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
}
}
Thank you for your help and patience.
Well this is that kind of situation where I end up heading my desk continously.
The source of the problem was I called dialog.dismiss() which dismisses the dialog, BUT not the dialogfragment itself, so will never, ever dismissed, even if the dialog disappeared from screen. Placing this.dismiss() in NoticeDialogFragment's onDismiss or anywhere else after login succeded will let the application act as it should.
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
this.dismiss(); //will dismiss the DialogFragment. Yeeey!
}
Thank you for your time and answers as they helped me point out the real problem. I will modify the code based on your suggestions.
An easier way is to use a static variable in your activity using two steps.
Declare a global static boolean
private static boolean session = false;
Check if the boolean has changed and if not, set the boolean to true when the dialog is shown
public void shownoticeDialog(){
if(session)return;
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
session = true;
}
Set the value when the activity goes background
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("authUser", GlobalInformations.getInstance().getUsername()==null)
}
and read it when it comes back
#Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null && savedInstanceState.containsKey("authUser")) {
boolean authUser = savedInstanceState.getBoolean("authUser", false);
if(authUser) {
//show or don't show dialog
}
}
}
mSomeFragment = new SomeFragment();
mSomeFragment.show(getFragmentManager(), "some");
The Fragment shows fine.
mSomeFragment = new SomeFragment();
mSomeFragment.show(getFragmentManager(), "some");
mSomeFragment.onDismiss(new DialogInterface() {
#Override
public void cancel() {
//
}
#Override
public void dismiss() {
//
}
});
But when I set onDismiss, this doesn't work (the Fragment doesn't shows). I wanna do some operations when the dialog dismisses.
Could you tell me why??
Calling onDismiss actually calls this code
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
which dismisses the dialog. If you want to listen for events on the dialog use onOptionsItemSelected()
A similar question has been asked before, but my case is different.
I have DialogFragments all over my app. When I rotate the phone, all of the DialogFragments come back without issue except this one.
I've littered the life cycle callbacks with Log messages to see what is going on, and this is the scenario:
My DialogFragment is created and shown
On rotation, I save whatever I want to into a bundle for restoration afterwards.
DialogFragment is successfully recreated. I know because onCreate through to onResume are called.
Immediately after resumption, for some inexplicable reason, onPause, onStop, onDestroyView, onDestroy and onDetach are called in rapid succession. The DialogFragment is destroyed immediately after recreation and I don't know why.
Any help is much appreciated. The DialogFragment starts an activity for result to take a picture. It works well for most phones, but the Galaxy S3 camera causes orientation changes that force the activity to be recreated. I don't mind this, I know how to handle activity recreation, but this I've never encountered.
The DialogFragment is started via a RecyclerView adapter callback from a regular fragment, in the main hosting activity.
I do not show the DialogFragment using the ChildFragmentManger in the fragment hosting the RecyclerView because multiple fragments can show this DialogFragment and the function is always the same. It was much more prudent to have the activity receive the callback regardless of which fragment started it.
From the fragment:
selectionPickAdapter.setAdapterListener(new selectionPickAdapter.AdapterListener() {
#Override
public void onSelectionClicked(Selection selection) {
if (getActivity() instanceof RankingActivity) {
((RankingActivity) getActivity()).onSelectionClicked(selection);
}
}
});
The main hosting activity receives the call back and shows it thus:
#Override
public void onSelectionClicked(Selection selection) {
if (isSignedInUser) {
selectionsToEdit.put(selection.hashCode(), selection);
if (baseCategory != null) {
selection.setCategory(baseCategory);
}
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
rankingSelectionEditDialogFragment.show(getSupportFragmentManager(), EDIT_TAG);
}
else {
Intent i = new Intent(this, BusinessActivity.class);
i.putExtra(Constants.BUSINESS, selection.getBusiness().getId());
startActivity(i);
}
}
These are my lifecycle callbacks:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
selectionToEdit = savedInstanceState.getParcelable(SELECTION_TO_EDIT);
imagePath = savedInstanceState.getString(IMAGE_PATH);
}
else {
selectionToEdit = getArguments().getParcelable(SELECTION_TO_EDIT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
Log.i(TAG, "CREATED A SECOND TIME!");
}
else {
Log.i(TAG, "CREATED ONCE!");
}
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_ranking_edit, container, false);
initializeViewComponents(rootView);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupFragment(selectionToEdit);
}
#Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
Dialog dialog = getDialog();
if (dialog != null) { // Only do this if returning a dialog, not a fragment
Log.i(TAG, "Dialog is not null");
SharedPreferences sharedPreferences
= getActivity().getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
// Get items required to put dialog just under the ActionBar.
int screenWidth = sharedPreferences.getInt(Constants.SCREEN_WIDTH, 720);
int screenHeight = sharedPreferences.getInt(Constants.SCREEN_HEIGHT, 1280);
int screenDPI = sharedPreferences.getInt(Constants.SCREEN_DPI, 320);
Window window = dialog.getWindow();
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
windowLayoutParams.y = -((screenHeight / 2) - 56) * (screenDPI / 160);
window.setAttributes(windowLayoutParams);
if (dialog.isShowing()) {
Log.i(TAG, "Dialog is showing");
}
else {
Log.i(TAG, "Dialog is not showing");
}
}
else {
Log.i(TAG, "Dialog is null");
}
Log.i(TAG, "onResume finished");
}
/**
* The system calls this only when creating the layout in a dialog.
*/
#Override
#NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
SharedPreferences sharedPreferences
= getActivity().getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
// Get items required to put dialog just under the ActionBar.
int screenWidth = sharedPreferences.getInt(Constants.SCREEN_WIDTH, 720);
int screenHeight = sharedPreferences.getInt(Constants.SCREEN_HEIGHT, 1280);
int screenDPI = sharedPreferences.getInt(Constants.SCREEN_DPI, 320);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
windowLayoutParams.y = -((screenHeight / 2) - 56) * (screenDPI / 160);
windowLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowLayoutParams);
return dialog;
}
/**
* Restore the previous currentFragment before the dialog was brought up
*/
#Override
public void dismiss() { // Used when the user deliberately dismisses the dialog
Log.i(TAG, "Dismissed");
super.dismiss(); // Ensure Super class method is called
}
/**
* Restore the previous currentFragment before the dialog was brought up
*/
#Override
public void onCancel(DialogInterface dialog) { // Used when the user inadvertently leaves the dialog,
// e.g back pressed or touched outside the dialog
Log.i(TAG, "Cancelled");
super.onCancel(dialog); // Ensure Super class method is called
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SELECTION_TO_EDIT, selectionToEdit);
outState.putString(IMAGE_PATH, imagePath);
}
#Override
public void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
#Override
public void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
#Override
public void onDestroyView() {
Log.i(TAG, "View Destroyed");
super.onDestroyView();
}
#Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
#Override
public void onDetach() {
Log.i(TAG, "onDetach");
super.onDetach();
}
EDIT: I've fixed the issue. To show the DialogFragment, I should use the ChildFragmentManager of the hosting fragment and not the activity. That is, changing this:
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
rankingSelectionEditDialogFragment.show(getSupportFragmentManager(), EDIT_TAG);
to this:
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
switch (currentFragment) {
case CATEGORY_PICK:
rankingCategoryPickFragment = (RankingCategoryPickFragment)
getSupportFragmentManager().findFragmentByTag(CATEGORY_PICK_TAG);
if(rankingCategoryPickFragment != null) {
rankingSelectionEditDialogFragment.show
(rankingCategoryPickFragment.getChildFragmentManager(), EDIT_TAG);
}
break;
case BUSINESS_SORT:
rankingBusinessSortParentFragment = (RankingBusinessSortParentFragment)
getSupportFragmentManager().findFragmentByTag(BUSINESS_SORT_TAG);
if(rankingBusinessSortParentFragment != null) {
rankingSelectionEditDialogFragment.show
(rankingBusinessSortParentFragment.getChildFragmentManager(), EDIT_TAG);
}
break;
was the ticket. Hope that helps anybody else with a similar issue.
in the following code work properly and show help screen when open activity but I want show one time forever,
what can i do?
What should I add in the code?
my code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
}
You can use SharedPrefereces to set a variable that will check if you've shown the dialog yet to the user or not, here's an example:
SharedPreferences prefs = this.getSharedPreferences("com.you.app", Context.MODE_PRIVATE);
Boolean dialogShown = prefs.getBoolean("dialogShown", false);
Then check if the value of dialogShown is false (you don't need to set it first since it will default to false the way we are calling it), then on the following code we execute some code, only if dialogShown is false, meaning we can do all the dialog stuff inside that conditional:
if(!dialogShown){
//Your show dialog code
prefs.edit().putBoolean("dialogShown",true).commit();
}
So the next time we check for the dialogShown value on the shared preferences it will be true therefor not showing the dialog. I believe this is the most common way of doing it.
There is a solution ..
when application first time start then save the shared preference to the app..
Now each and every time You retrieve the shared preference and check if it is there then move to next screen
Use this code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
Boolean showOneTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
if (showOneTime == false) {
return;
}
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
showOneTime = false;
}
}
I'm having the following issue developing in android 2.2 (API 8):
I have a customized Dialog class like this:
public AuthDialog(final Context context, OnDismissListener dismissListener, OnCancelListener cancelListener) {
super(context);
setOnDismissListener(dismissListener);
setOnCancelListener(cancelListener);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpassdialog);
setTitle("Enter email and password");
setCancelable(true);
setCanceledOnTouchOutside(true);
authEmail = (EditText) findViewById(R.id.authEmail);
authPass = (EditText) findViewById(R.id.authPass);
alertMessage = (TextView) findViewById(R.id.auth_alert);
Button authButton = (Button) findViewById(R.id.authButton);
View.OnClickListener onClickListener = new View.OnClickListener() {
public void onClick(View v) {
if (checkCredentials())
dismiss();
else
showAlert();
}
};
authButton.setOnClickListener(onClickListener);
}
private void showAlert() {
alertMessage.setText("Wrong user/pass");
authEmail.setText(null);
authPass.setText(null);
}
private boolean checkCredentials() {
// Empty user/pass for now
boolean checkEmail = authEmail.getText().toString().equals("");
boolean checkPassword = authPass.getText().toString().equals("");
return checkEmail && checkPassword;
}
#Override
public void onBackPressed() {
cancel();
}
And I create a new AuthDialog like this:
private void authenticateThenAccept() {
OnDismissListener dismissListener = new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
accept();
}
};
OnCancelListener cancelListener = new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
cancel();
}
};
AuthDialog dialog = new AuthDialog(context, dismissListener, cancelListener);
dialog.show();
}
I'm using the debugger, and I see that when I cancel (using the back button or pressing outside the dialog) the app dismisses the dialog instead of cancelling.
Anybody has had this kind of issue with Dialogs?
Thanks in advanced.
onDismiss() is always fired when dialog closes. The documentation for setOnCancelListener() states: "This will only be invoked when the dialog is canceled, if the creator needs to know when it is dismissed in general, use setOnDismissListener", i.e. it's not either onCancel or onDismiss but both when a dialog is canceled. I agree though that it would have made more sense had that not been the case.
Assuming this dialog should be modal, make your dialog a new activity.
setCancelable(false) will prevent the back button from doing anything. Many developers just turn off the ability of the back button to close the dialog since it's unclear whether that is a cancel or ok action to the user.