eltos / SimpleDialogFragments

An Android library to create dialogs with ease and handle user interaction reliably, using fragments and material design.
Apache License 2.0
119 stars 17 forks source link

How to use your library if I do not extend a FragmentActivity? #16

Closed isabsent closed 6 years ago

isabsent commented 6 years ago

I am trying to adopt your lib for my project in which I need to show some dialogs in the android.app.Activity but not the FragmentActivity.

    SimpleListDialog.build()
            .title(R.string.dialog_title_attention)
            .choiceMode(SimpleListDialog.SINGLE_CHOICE_DIRECT)
            .items(getBaseContext(), new int[]{R.string.choice_A, R.string.choice_B, R.string.choice_C})
            .show(???????????????????, SimpleListDialog.class.getName());

What should I write at the first argument of the show method?

eltos commented 6 years ago

I recommend that you use AppCompatActivity from the support-library (which my library relies on) instead of android.app.Activity. This will also allow you to use newer features and material design on older android versions. It extends the core android.app.Activity, thus you should be able to just replace it without any compatibility issues to your existing code. You only need to change the getFragmentManager() calls to getSupportFragmentManager() in case you are using it.

android.app.Activityandroid.support.v4.app.FragmentActivityandroid.support.v7.app.AppCompatActivity

eltos commented 6 years ago

Any update on this @isabsent?

isabsent commented 6 years ago

It is acceptable for new project, but I would not like to change existing code now. Would you be so kind to made a fork of your library with android.app.Activity?

I have tried to remove all support dependencies from your code. It is quite simple. The problem exists only for resultFragment = resultFragment.getParentFragment() in eltos.simpledialogfragment.SimpleDialog class. It requires API 17 to use a method getParentFragment(). Is it possible to change only this part of your code somehow to make API level little bit lower? API 14 would be nice :)

Based on

        Fragment resultFragment = getTargetFragment();
        while (!handled && resultFragment != null){
            if (resultFragment instanceof OnDialogResultListener){
                handled = ((OnDialogResultListener) getTargetFragment())
                        .onResult(getTag(), which, extras);
            }
            resultFragment = resultFragment.getParentFragment();
        }

I can suppose you need resultFragment = resultFragment.getParentFragment() only if SimpleDialog was called from other Fragment and therefore if I call your dialogs from an Activity and not from Fragment this part of code will never used. Am I right?

eltos commented 6 years ago

I think it is much easier for you to change to the support library (i.e. adding functionality) instead of me trying to make the library work without it (i.e. removing functionality). I don't think rewriting the library is that simple and I will not spend time on this (I don't have much free time momentarly anyways and if you don't want to change existing code then why would I change the whole library?). However, it seems that you already put some effort into this. So please feel free to continue your Work; I will happily accept a merge request if you find a propper solution.

Concerning your last question: Yes, that is exactly what happens here. You might want to check the api-level and call this function only on devices running 17+.

isabsent commented 6 years ago

I have ended with this code in eltos.simpledialogfragment.SimpleDialog:

        while (!handled && resultFragment != null){
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                if (resultFragment instanceof OnDialogResultListener) {
                    handled = ((OnDialogResultListener) getTargetFragment())
                            .onResult(getTag(), which, extras);
                }
                resultFragment = resultFragment.getParentFragment();
            } else {
                Toast.makeText(getActivity(), "Do not use this fork of SimpleDialogFragment library from a Fragment if API < 17!", Toast.LENGTH_LONG).show();
                break;
            }
        }

for android.app.Activity and android.app.Fragmentinstead of support classes.

eltos commented 6 years ago

You do realise that this is an infinite loop on devices running API<17 and will not even call the target fragments onResult method, don't you?

isabsent commented 6 years ago

Thanks! Fixed by means of break; :)