user3492408
user3492408

Reputation: 21

how can show alert dialog in another activity?

I want to show an Alert Dialog non-activity class to a main activity class getversion method method but whenever I call AlertDialog via that method it generates error with following text

public void AlertUpgrade(Activity activity) {

    Log.e("AlertUpgrade", "Communicator.");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("  Click OK to Upgrade Now ? ")
        .setPositiveButton("OK" +
            "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpgrade");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=gsip.webgalaxy"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context.startActivity(marketIntent);  
        }
    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                return false;
            }

            return true;
        }
    });
    builder.create().show();  
}

public void AlertUpdate(Activity activity) {

    Log.e("AlertUpdate", "Communicator.....");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("There is newer version of this application available, click OK to update now?").setPositiveButton("OK" +
        "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpdate");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("url"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context. startActivity(marketIntent);  
        }
    }).setNegativeButton("Remind Later", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            // User cancelled the dialog
        }

    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
               if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                   return false;
               }

               return true;
           }
       });
    builder.create().show(); 
}

Error Log

04-03 09:04:44.007: E/AndroidRuntime(2249): java.lang.RuntimeException: Unable to start activity ComponentInfo{gsip.webgalaxy/gsip.webgalaxy.ui.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Looper.loop(Looper.java:136)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invoke(Method.java:515)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at dalvik.system.NativeStart.main(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Dialog.show(Dialog.java:286)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.Communicator.getversion(Communicator.java:226)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.MainActivity.onCreate(MainActivity.java:460)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Activity.performCreate(Activity.java:5231)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 09:04:44.007: E/AndroidRuntime(2249):     ... 11 more
04-03 09:09:44.177: I/Process(2249): Sending signal. PID: 2249 SIG: 9

Upvotes: 2

Views: 3364

Answers (2)

Saran
Saran

Reputation: 3885

One possiblility would be to specify some extras when starting the other Activity and then to intercept those extras in the target Activity and show the AlertDialog accordingly.

Something like this...

Source Activity:

  Intent intent = new Intent(this, TargetActivity.class);
  Bundle bundle = new Bundle();
  bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info);
  bundle.putString("alert_title", "Some Title");
  bundle.putString("alert_message", "Some message");
  intent.putExtras(bundle);
  startActivity(intent);

Target Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle b = getIntent().getExtras();
  if (b != null && b.containsKey("alert_icon_res_id")) {
    int icon = b.getInt("alert_icon_res_id");
    int title = b.getString("alert_title");
    int message = b.getString("alert_message");

    new AlertDialog.Builder(this).setIcon(icon)
      .setTitle(title).setMessage(message)
      .setPositiveButton(R.string.edit_dialog_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      }).create().show());
   }
}

Upvotes: 2

laalto
laalto

Reputation: 152817

You need an Activity Context for dialogs. If you're showing it from a non-activity class, pass in a reference to the Activity as an argument.

Upvotes: 0

Related Questions