4

I have overriden the onSavedInstanceState and removed the super, i am not doing any transactions, let alone transactions in volatile functions or async ones.

I am calling it in a custom callback 'onUserExistsListener'

        @Override
        public Boolean userExists(Boolean exists) {
            if (exists) {


            }
            else
            {
             AlertDialogFragment Frag = AlertDialogFragment.newInstance(0, null);
             Frag.show(getSupportFragmentManager(), "warndialog");
            }
   };

Have tried everything suggested in the other SO questions and still found no solution? Anyone?

code:

AlertDialogFragment Frag = AlertDialogFragment.newInstance(0, null);
 Frag.show(getSupportFragmentManager(), "warndialog");

The process:

OnCreate: (LoadNames function)

LoadNames Callback -> Show dialog.

Further:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        add_name_to_db();
    }

function:

public void add_name_to_db() {
        Details.checkNameExists(user_txt.getText().toString());
        Details.setOnUserExistsListener(new onUserExistsListener() {

            @Override
            public Boolean userExists(Boolean exists) {

                if (exists) 
                    {

                    } else {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                AlertDialogFragment Frag = AlertDialogFragment.newInstance(0, null);
                                Frag.show(getSupportFragmentManager(), "warndialog");

                            }

                        });

                    }
                } else {
                    Toast.makeText(getApplicationContext(), "This is not a valid KIK username",
                            Toast.LENGTH_SHORT).show();
                }

                return exists;
            }

        });

    }
Broak
  • 4,161
  • 4
  • 31
  • 54
  • Can you please clarify your question some more? – Eric Woodruff Jan 25 '14 at 00:02
  • Are you not calling super.onCreate()? You have to do that. – MikeHelland Jan 25 '14 at 04:18
  • The question is not very clear, you should show where are you calling `onUserExistsListener`. Also, you don't need to override `onSavedInstanceState`. Maybe this blog post can help you understand better why this Exception occurs and how to avoid it -> http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html – rubenlop88 Jan 25 '14 at 05:52
  • @rubenlop88 Code added! It must have to do with the call back somehow, i just cant figure out why. – Broak Jan 25 '14 at 12:29
  • Are you getting a stacktrace in the log, is it crashing? If so amend your question... :) – t0mm13b Jan 25 '14 at 13:13
  • java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at the 'Frag.show' – Broak Jan 25 '14 at 16:11
  • Are you calling finish () some where? – MikeHelland Jan 26 '14 at 00:13
  • OP - please include the full stacktrace... to save us all from guessing... – t0mm13b Jan 26 '14 at 02:46
  • The callback is being called "later on", specifically after the activity has been destroyed or in your case, saved/hidden. – Sherif elKhatib Jan 29 '14 at 23:17
  • "I have overriden the onSavedInstanceState and removed the super, i am not doing any transactions, let alone transactions in volatile functions or async ones." -> The question is more rather: WHY ARE YOU DOING THIS? – Martin Marconcini Jan 31 '14 at 06:11
  • In an attempt to find a solution, they we're not already implemented and have since been reversed. – Broak Jan 31 '14 at 11:01
  • possible duplicate of [Show DialogFragment from onActivityResult](http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult) – rds Aug 18 '15 at 10:53

3 Answers3

6

By calling Frag.show(getSupportFragmentManager(), "warndialog");, you are in fact performing a fragment transaction (see show()).

If the activity's onSaveInstanceState() method has been called, an IllegalStateException will be thrown when you try to show the dialog. You need to change your code to ensure that when you show the dialog fragment, you do so after the activity state has been restored (i.e., onPostResume()). See this blog post for more information.

You can also commit the transaction allowing state loss;

Luis
  • 3,451
  • 1
  • 27
  • 41
  • 1
    Unfortunately, commiting allowing stateloss leaves me with : java.lang.IllegalStateException: Activity has been destroyed – Broak Jan 27 '14 at 11:13
  • 1
    If the activity has been destroyed then you can't attach a fragment to it.Either, change to a regular dialog or ensure you commit with the activity alive and when the activity's state has been restored. – Luis Jan 27 '14 at 20:18
  • I've made an alternative solution to DialogFragment, that can avoid this exception : https://github.com/AndroidDeveloperLB/DialogShard – android developer Oct 25 '16 at 08:10
0

EDIT: You need to call the super.onCreate(SavedInstanceState) in onCreate(). It just won't work without it.


onSaveInstanceState() occurs when the activity is finishing. It's too late to stop and show UI there.

When do you want this code to run? When the back button is pressed maybe?

Put your code in onBackPressed() for that.

Android: Proper Way to use onBackPressed() with Toast

EDIT If you want the code to run at the beginning of an activity, as you mention in another comment. Don't use onSaveInstanceState(). Put your code in onCreate() or onResume().

Community
  • 1
  • 1
MikeHelland
  • 1,151
  • 1
  • 7
  • 17
  • I'm not using saved instance state what so ever, im trying to display a dialog in a normal function when the activity is nowhere near the point of going on pause or into the saved instance. – Broak Jan 25 '14 at 01:37
  • You need to call the super.onCreate(SavedInstanceState) in onCreate(). It just won't work without it. – MikeHelland Jan 25 '14 at 01:48
  • I'm already doing that. Sorry if i'm confusing you some how? All have another look at it now, and see if i can shed some light on it with fresh mind. – Broak Jan 25 '14 at 03:00
-1

Depending on how your callback is working, you may need to run those lines of code on the UI Thread, like this:

runOnUiThread(new Runnable() {
    public void run() {
        AlertDialogFragment Frag = AlertDialogFragment.newInstance(0, null);
        Frag.show(getSupportFragmentManager(), "warndialog");

    }

});
MikeHelland
  • 1,151
  • 1
  • 7
  • 17