简体   繁体   中英

Activity is not waiting for alert dialog

I am actually a C# developer but currently developing an app for an android with java.

Well,I wanted to create a general dialog alert class and has a static method that is given title,message as parameter and shows an alert. it seems everything OK. Here is the my class..

public class AlertDialogHelper  {

public static void setOkMessage(Context context,String title,String message)
{

    AlertDialog dialog = new AlertDialog.Builder(context).create();
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setButton("OK", new DialogInterface.OnClickListener()  {

        @Override
        public void onClick(DialogInterface dialog, int which) {

             // NOTHING??

        }
    });     

    dialog.setIcon(R.drawable.logo);
    dialog.show();
 }
}

and I called this static method in my splashactivity, but when alert dialog appeared, after code is going on that means execute codes below..

private void closeSplashActivity() {

 Intent activity;
 boolean isNew=true;

  try{

    DbHelper dbHelper = new DbHelper(this);
    dbHelper.openDbConnection();
    isNew = dbHelper.getSettings(DbContext.COLUMN_ISNEW);
    dbHelper.closeDbConnection();

   if (isNew)
   AlertDialogHelper.setOkMessage(this, "XXX", "YYYY");

   else
       AlertDialogHelper.setOkMessage(this, "XXXXX", "YYYYY");



 } catch (Exception ex) {

AlertDialogHelper.setOkMessage(this, "Error", "XXXXX:" + ex.getMessage());
        ex.printStackTrace();
 }

   if(isNew) 
   activity = new Intent(SplashActivity.this, ChooseQualityActivity.class); 

   else 
   activity = new Intent(SplashActivity.this, HomeActivity.class);

   startActivity(activity);
   finish();

 }

Normally, It must wait for the onclick event and after that

    if(isNew) 
   activity = new Intent(SplashActivity.this, ChooseQualityActivity.class);

must work.. but, it doesnt wait and run this code above.. So, I do not know what I can do it for it.. And Is there another method for creating general dialog box? Because it looks so long code to create for each message.

@Edit..

I have just realized that some exception in logcat

01-30 23:07:47.375: E/WindowManager(9191): Activity com.aaa.bbbb.SplashActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2c12e008 VE.... R.....ID 0,0-348,285} that was originally added here 01-30 23:07:47.375: E/WindowManager(9191): android.view.WindowLeaked: Activity com.aaa.bbbb.SplashActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2c12e008 VE.... R.....ID 0,0-348,285} that was originally added here 01-30 23:07:47.375: E/WindowManager(9191): at android.view.ViewRootImpl.(ViewRootImpl.java:354) 01-30 23:07:47.375: E/WindowManager(9191): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216) 01-30 23:07:47.375: E/WindowManager(9191): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 01-30 23:07:47.375: E/WindowManager(9191): at android.app.Dialog.show(Dialog.java:281) 01-30 23:07:47.375: E/WindowManager(9191): at com.aaa.bbbb.utility.AlertDialogHelper.setOkMessage(AlertDialogHelper.j ava:31) 01-30 23:07:47.375: E/WindowManager(9191): at com.aaa.bbbb.SplashActivity.closeSplashActivity(SplashActivity.java:65) 01-30 23:07:47.375: E/WindowManager(9191): at com.aaa.bbbb.SplashActivity.access$0(SplashActivity.java:53) 01-30 23:07:47.375: E/WindowManager(9191): at com.aaa.bbbb.SplashActivity$1.onCompletion(SplashActivity.java:40) 01-30 23:07:47.375: E/WindowManager(9191): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1980) 01-30 23:07:47.375: E/WindowManager(9191): at android.os.Handler.dispatchMessage(Handler.java:99) 01-30 23:07:47.375: E/WindowManager(9191): at android.os.Looper.loop(Looper.java:137) 01-30 23:07:47.375: E/WindowManager(9191): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-30 23:07:47.375: E/WindowManager(9191): at java.lang.reflect.Method.invokeNative(Native Method) 01-30 23:07:47.375: E/WindowManager(9191): at java.lang.reflect.Method.invoke(Method.java:511) 01-30 23:07:47.375: E/WindowManager(9191): at com. android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-30 23:07:47.375: E/WindowManager(9191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-30 23:07:47.375: E/WindowManager(9191): at dalvik.system.NativeStart.main(Native Method)

Ok, first you have to define an interface like this:

interface MyCallback {
    void callbackCall();
}

Then in your AlertDialogHelper class include a member, like this:

MyCallback mCallback;

I your set OkMessageMethod add a parameter

public static void setOkMessage(Context context,String title,String message, MyCallback callback) {
mCallback = callback

later in onClick method you call like this:

callback.callbackCall();

the rest is easy, just call the setOkMessage this way:

AlertDialogHelper.setOkMessage(this, "Error", "XXXXX:" + ex.getMessage(), new MyCallback(){
 // Here the code you want to execute after the click event
});

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM