The code causes a malfunction in some phones and works on the emulator. Is there an explanation?

Viewed 25
  • This is the error in the Google Play Store
java.lang.IllegalArgumentException: 
  at android.view.WindowManagerGlobal.findViewLocked (WindowManagerGlobal.java:572)
  at android.view.WindowManagerGlobal.removeView (WindowManagerGlobal.java:476)
  at android.view.WindowManagerImpl.removeViewImmediate (WindowManagerImpl.java:144)
  at android.app.Dialog.dismissDialog (Dialog.java:389)
  at android.app.Dialog.dismiss (Dialog.java:371)
  at com.master.azkarmorningevening.LoadingDialog.dismissDialog (LoadingDialog.java)
  at com.master.azkarmorningevening.-$$Lambda$hxi6xW-58LrFqQAbkmexCKkt5eY.run(lambda)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:236)
  at android.app.ActivityThread.main (ActivityThread.java:8060)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:656)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:967)
  • The class that i create Dialog

    class LoadingDialog {
          Activity activity;
          private AlertDialog dialog;
          LoadingDialog(Activity myActivity){
              activity = myActivity;
          }
    
    
          @SuppressLint("InflateParams")
          void StartLoadingAlertDialog(){
              AlertDialog.Builder builder = new 
             AlertDialog.Builder(activity,R.style.full_sereen_mood);
    
              LayoutInflater inflater = activity.getLayoutInflater();
              builder.setView(inflater.inflate(R.layout.splash,null));
              builder.setCancelable(false);
    
              dialog = builder.create();
    
    
              dialog.show();
          }
    
    
          void dismissDialog(){
                  dialog.dismiss();
    
          }
      }
    
  • i get function from class LoandinDailog

  • with Handler

    public class MainActivity extends AppCompatActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    
          LoadingDialog loadingDialog = new LoadingDialog(MainActivity.this);
          loadingDialog.StartLoadingAlertDialog();
          Handler handler = new Handler(Looper.getMainLooper());
          handler.postDelayed(loadingDialog::dismissDialog,3000);
    
    
    
      }
    

    }

  • Make Style for Dialog for class LoadingDialog

  • To define some properties

parent="Theme.AppCompat.DayNight.DarkActionBar"

    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:windowFullscreen">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>
1 Answers

Short answer: You need to place the part below inside onResume()

  LoadingDialog loadingDialog = new LoadingDialog(MainActivity.this);
  loadingDialog.StartLoadingAlertDialog();
  Handler handler = new Handler(Looper.getMainLooper());
  handler.postDelayed(loadingDialog::dismissDialog,3000);

With that done, the splash screen will appear every time the UI thread resumes, which is going to be annoying, so you'll need to have a flag that tells you whether the splash screen was already shown, or not, and check for that condition before you execute that block of code inside onResume()

Longer answer: You need to watch some videos on youtube from Google IO about the Activity lifecycle. Those Android fundamentals are very important and they can not be fully explained in a page or two.

https://www.youtube.com/watch?v=bEKNi1JOrNs

Related