Android State State Loss

Can someone answer this question for me:

For testing purposes, I created an action with a for loop in which I create 10 AlertDialogs or 10 DialogFragments. Immediately after starting the activity, I press the "home" button to send the application in the background. If I run the showDialog () method to create a Dialog dialog box, the application will crash with:

IllegalStateException: Can not perform this action after onSaveInstanceState 

this is the expected behavior.

But if I run the showAlert () method to create AlertDialogs and the same way as before sending the application to the background, the application does not crash. When I return to activity, I will see all 10 AlertDialogs.

The question is why does state loss happen with DialogFragment and not with AlertDialog?

I am still changing the user interface after maintaining the activity state. The platform I tested on is Android 4.4.2.

 public class Main extends FragmentActivity { private FragmentActivity activity = this; private MyAsynk myAsynk; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); myAsynk = new MyAsynk(); myAsynk.execute(); } private class MyAsynk extends AsyncTask<Void, Void, Void> { private boolean run = false; public MyAsynk() { run = true; } @Override protected Void doInBackground(Void... params) { for(int i = 0; i < 10 && run; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // showAlert("loop " + i); showDialog("loop " + i); } return null; } public void stop() { run = false; } } @Override public void onBackPressed() { super.onBackPressed(); if(null != myAsynk) { myAsynk.stop(); myAsynk = null; } } private void showAlert(final String txt) { try { Main.this.runOnUiThread(new Runnable() { @Override public void run() { new AlertDialog.Builder(activity).setMessage(txt) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { try { if(null != dialog) { dialog.dismiss(); } } catch(Exception e) { e.printStackTrace(); } } }) .show(); } }); } catch(Exception e) { e.printStackTrace(); } } private void showDialog(final String txt) { try { Main.this.runOnUiThread(new Runnable() { @Override public void run() { MyDialogFragment newFragment = MyDialogFragment.newInstance(txt); FragmentTransaction ft = Main.this.getSupportFragmentManager().beginTransaction(); newFragment.show(ft, "newFragment"); } }); } catch(Exception e) { e.printStackTrace(); } } } 

MyDialogFragment.java:

 public class MyDialogFragment extends DialogFragment { private MyDialogFragment instance; public static MyDialogFragment newInstance(String text) { MyDialogFragment f = new MyDialogFragment(); Bundle args = new Bundle(); args.putString("text", text); f.setArguments(args); return f; } public MyDialogFragment() { instance = this; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.my_dialog_fragment, container, false); TextView tv = (TextView) v.findViewById(R.id.tv); Button bu = (Button) v.findViewById(R.id.bu); bu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if(null != instance && instance.isVisible()) { instance.dismiss(); } } catch(Exception e) { e.printStackTrace(); } } }); tv.setText(getArguments().getString("text")); return v; } } 
+5
source share
1 answer

The answer is very simple, although a little overwhelming.

Exception java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState actually java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState by the FragmentManager class. The reason it is very well explained in this post by Alex Lockwood .

DialogFragments are fragments (and thus managed by the FragmentManager ). Therefore, showing dialogs in this way may throw an exception. However, the implementation of AlertDialog completely different: it does not use fragments at all (in fact, it actually precedes fragments). Therefore, no exceptions.

+1
source

Source: https://habr.com/ru/post/1208988/


All Articles