Question

I have a little question. Currently I have a fragment. When detaching I want to show the user something like do you want to save the changes. But I want to call super.onDetach() after the user has selected an option. I tried to call super.onDetach() after an option but then I get an exception that super.onDetach() is not called when detaching. Any ideas?

Thanks in advance

This is my code :

@Override
public void onDetach() {


    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){

            case DialogInterface.BUTTON_POSITIVE:
                if(attachProfiel){
                    FragmentProfiel.updateProfiel();
                }
                if(attachDiagnose){
                    FragmentDiagnose.updateDiagnose();
                }
                if(attachAndere){
                    FragmentAndere.updateAndere();
                }
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Save changes?").setPositiveButton("Ja", dialogClickListener)
        .setNegativeButton("Nee", dialogClickListener).show();


}

OnDestroyView :

@Override
public void onDestroyView() {
    new ExitFragment(getActivity()).show(getFragmentManager(), "MyDialog");
    super.onDestroyView();

}
Was it helpful?

Solution

I would recommend that you call your DialogFragment() in onBackPressed() method, there by asking the user to save the preference. If you would like to reclaim the data that user entered in the fragment, then use SharedPreference in android. See http://developer.android.com/reference/android/content/SharedPreferences.html

OTHER TIPS

I would say you should not go that way. This kind of mobile design is not safe. The only way to implement this is to block the Ui thread for some time until the user presses something in dialog or a fixed time expires. But then presume that while the app blocks the user gets a phone call or even worse the phone call comes in when the dialog is about to show. It would be a nightmare.

best way to approach this is to periodically (temp)save state of screen and then ask you that he has unsaved changes in the onresume of next fragment or activity. For things to run smoothly android does need to be able to shut down your app whenever it needs to. If you try to circumvent that control then its a ugly mess ahead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top