title: Android could not lock surface when puting shared preferences
body: <p>Very strange thing. I found out that when I do <code>spEditor.putBoolean(&quot;closePxCmInfo&quot;, false).apply();</code> then an error occurs like that:</p>
  <pre><code>2022-01-03 10:47:39.761 3821-3917/? E/ViewRootImpl@6827b15[mierzenieopencv]: Surface is not valid.
  2022-01-03 10:47:39.774 3821-3917/? E/ViewRootImpl@f395385[mierzenieopencv]: Surface is not valid.
  2022-01-03 10:47:39.792 3821-3917/? E/ViewRootImpl@24f27f5[mierzenieopencv]: Surface is not valid.
  2022-01-03 10:47:39.820 13383-13710/pl.jawegiel.mierzenieopencv E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
  2022-01-03 10:47:39.824 3335-3449/? E/BufferQueueProducer: [Splash Screen pl.jawegiel.mierzenieopencv[13383]#0] connect: BufferQueue has been abandoned
  2022-01-03 10:47:39.824 3821-3917/? E/ViewRootImpl@4fd5d5c[mierzenieopencv]: Could not lock surface
      java.lang.IllegalArgumentException
          at android.view.Surface.nativeLockCanvas(Native Method)
          at android.view.Surface.lockCanvas(Surface.java:345)
          at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:4018)
          at android.view.ViewRootImpl.draw(ViewRootImpl.java:3979)
          at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3721)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3029)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8511)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
          at android.view.Choreographer.doCallbacks(Choreographer.java:761)
          at android.view.Choreographer.doFrame(Choreographer.java:696)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:214)
          at android.os.HandlerThread.run(HandlerThread.java:65)
          at com.android.server.ServiceThread.run(Unknown Source:12)
  </code></pre>
  <p>It has nothing to do with my lauout I tested it out. The problem is with this line that I mentioned about putting shared preference.</p>
  <p>Here is my code:</p>
  <p>Stages.java</p>
  <pre><code>public class Stages extends BaseActivity {
  
  private final Stage0StageFragment stageZeroFragment = new Stage0StageFragment();
  private final Stage1StageFragment stageOneFragment = new Stage1StageFragment();
  private final Stage2StageFragment stageTwoFragment = new Stage2StageFragment();
  private final Stage3StageFragment stageThreeFragment = new Stage3StageFragment();
  
  private BottomNavigationView bottomNavView;
  private int startingPosition, newPosition;
  OnSwitchFragmentFromStageTwo onSwitchFragmentFromStageTwo;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Util.setThemeBasedOnPrefs(this);
      setContentView(R.layout.stages);
  
      spEditor.putBoolean(&quot;closePxCmInfo&quot;, false).apply();          // HERE IS THE PROBLEM
  
      bottomNavView = findViewById(R.id.bottom_navigation);
      bottomNavView.setItemIconTintList(null);
      bottomNavView.setOnNavigationItemSelectedListener(item -&gt; {
  
          if (bottomNavView.getSelectedItemId() == R.id.navigation_stage_zero) {
              if (!sp.getBoolean(&quot;correctionDone&quot;, false)) {
                  AlertDialog alertDialog = Util.createAlertDialog(Stages.this, android.R.drawable.ic_dialog_info, R.string.hide_dialog_title, getString(R.string.stage_zero_confirmation), (dialog, which) -&gt; {
                      spEditor.putBoolean(&quot;correctionDone&quot;, true).apply();
                      bottomNavView.setSelectedItemId(item.getItemId());
                      showFragment(item.getItemId());
                  });
                  alertDialog.setOnShowListener(arg0 -&gt; setAlertDialogButtonsAttributes(alertDialog));
                  alertDialog.show();
                  return false;
              } else {
                  showFragment(item.getItemId());
                  return true;
              }
          } else if (bottomNavView.getSelectedItemId() == R.id.navigation_stage_two) {
              try {
                  if (onSwitchFragmentFromStageTwo.onSwitchFragmentFromFragmentTwo() &lt;= 0.5 &amp;&amp; !sp.contains(&quot;closePxCmInfo&quot;)) {
                      AlertDialog ad = Util.createAlertDialog(Stages.this, android.R.drawable.ic_dialog_alert, R.string.hide_dialog_title_alert, getResources().getString(R.string.benchmark_not_drawn), (dialog, which) -&gt; {
                          spEditor.putBoolean(&quot;closePxCmInfo&quot;, true).apply();
                          bottomNavView.setSelectedItemId(item.getItemId());
  
                          showFragment(item.getItemId());
                      });
                      ad.setOnShowListener(arg0 -&gt; setAlertDialogButtonsAttributes(ad));
                      ad.show();
  
                      return false;
                  } else {
                      showFragment(item.getItemId());
                      return true;
                  }
              } catch (PackageManager.NameNotFoundException e) {
                  e.printStackTrace();
              }
          } else {
              showFragment(item.getItemId());
          }
          return true;
      });
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      if (!sp.getBoolean(&quot;correctionDone&quot;, false))
          ft.replace(R.id.content_frame, stageZeroFragment);
      else {
          ft.replace(R.id.content_frame, stageOneFragment);
          bottomNavView.setSelectedItemId(R.id.navigation_stage_one);
      }
      ft.commit();
  }
  
  @Override
  BaseActivity getActivity() {
      return this;
  }
  
  private void setAlertDialogButtonsAttributes(AlertDialog alertDialog2) {
      alertDialog2.getButton(DialogInterface.BUTTON_NEGATIVE).setBackground(AppCompatResources.getDrawable(this, R.drawable.button_selector));
      alertDialog2.getButton(DialogInterface.BUTTON_POSITIVE).setBackground(AppCompatResources.getDrawable(this, R.drawable.button_selector));
      LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
      params.setMargins(10, 0, 10, 0);
      alertDialog2.getButton(DialogInterface.BUTTON_NEGATIVE).setLayoutParams(params);
      alertDialog2.getButton(DialogInterface.BUTTON_POSITIVE).setLayoutParams(params);
  }
  
  public void showFragment(int viewId) {
      Fragment fragment = null;
  
      switch (viewId) {
          case R.id.navigation_stage_zero:
              if (bottomNavView.getSelectedItemId() != R.id.navigation_stage_zero) {
                  fragment = stageZeroFragment;
                  newPosition = 0;
              }
              break;
          case R.id.navigation_stage_one:
              if (bottomNavView.getSelectedItemId() != R.id.navigation_stage_one) {
                  fragment = stageOneFragment;
                  newPosition = 1;
              }
              break;
          case R.id.navigation_stage_two:
              if (bottomNavView.getSelectedItemId() != R.id.navigation_stage_two) {
                  fragment = stageTwoFragment;
                  newPosition = 2;
              }
              break;
          case R.id.navigation_stage_three:
              if (bottomNavView.getSelectedItemId() != R.id.navigation_stage_three) {
                  fragment = stageThreeFragment;
                  newPosition = 3;
              }
              break;
      }
  
      if (fragment != null) {
          if (startingPosition &gt; newPosition) {
              getSupportFragmentManager()
                      .beginTransaction()
                      .setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right)
                      .replace(R.id.content_frame, fragment).commit();
          }
          if (startingPosition &lt; newPosition) {
              getSupportFragmentManager()
                      .beginTransaction()
                      .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
                      .replace(R.id.content_frame, fragment).commit();
          }
  
          startingPosition = newPosition;
      }
  }
  }
  </code></pre>
  <p>BaseActivity.java</p>
  <pre><code>public abstract class BaseActivity extends AppCompatActivity {
  
  SharedPreferences sp;
  SharedPreferences.Editor spEditor;
  BaseActivity childActivity;
  
  @Override
  @SuppressWarnings(&quot;CommitPrefEdits&quot;)
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      sp = PreferenceManager.getDefaultSharedPreferences(BaseActivity.this);
      spEditor = sp.edit();
      childActivity = getActivity();
  
      if(childActivity instanceof MainActivity)
          MobileAds.initialize(this, initializationStatus -&gt; {});
  }
  
  @Override
  protected void onPause() {
      if (childActivity instanceof Stages) {
          spEditor.putFloat(&quot;px_cm&quot;, 0f);
          spEditor.putFloat(&quot;rozmiar_px&quot;, 0f);
          spEditor.putFloat(&quot;rozmiar_px_x&quot;, (float) 0f);
          spEditor.putFloat(&quot;rozmiar_px_y&quot;, (float) 0f);
          spEditor.apply();
      }
      super.onPause();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      if (childActivity instanceof Stages) {
          getMenuInflater().inflate(R.menu.main, menu);
      }
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
      if (childActivity instanceof Stages)
          return Util.onOptionsItemSelected(this, item);
      else
          return false;
  }
  
      @Override
      public void onBackPressed() {
          if (childActivity instanceof Stages) {
              Intent intent = new Intent(childActivity, MainActivity.class);
  //            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
              startActivity(intent);
          }
          super.onBackPressed();
      }
  
  abstract BaseActivity getActivity();
  }
  </code></pre>
  <p>Help me please!</p>