2

I'm trying to setup UI where user will have to login first time they use applications. And Custom Dialog seems like a good thing to use as I want main UI to be kind of visible on background.

So, what I did - I created main Activity and use ShowDialog() with onCreateDialog from main activity.

I created public class LoginDialog extends Dialog implements View.OnClickListener and I can control all stuff on dialog just like activity.

Ideally I like to check if user logged in on main activity and if not - show this dialog. Otherwise just go with activity.

On dialog I'd like to log user in and if user clicks back without logging in - I want to detect it and just finish main activity. This is where I have problem.

  1. In WinForms I would do ShowDialog() (in C#) and next line executed when dialog closed for any reason. I can't figure out how to do this in Android.

  2. I didn't get to it yet, but I want to show progress bar when Login button clicked. This bar will be in Dialog box. Is it possible/doable?

Thanks in advance!

katit
  • 17,375
  • 35
  • 128
  • 256

1 Answers1

2

You can detect dialog dismissal using setOnDismissListener. In this method you could also call MyActivity.this.finish().

For showing a ProgressBar during the login process, you probably want to look at this answer. It shows the basic structure of AsyncTask and you can adapt it to use ProgressBar instead of ProgressDialog.

You would be changing the bar's visibility in onPreExecute and onPostExecute with bar.setVisibility(View.VISIBLE) and bar.setVisibility(View.INVISIBLE).

Edit

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        dialog.dismiss();
        if (!isLoggedIn()) {
            MyActivity.this.finish();
        }
    }
});

This code should be in your MyActivity wherever you create the dialog. You need to check to see if the user is logged in or not, because onDismiss will be called whether it's the user or your own code that closes the dialog.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • @Matthew Can you show me example on how to use (and where to use) setOnDismissListener. I can't find example anywhere – katit Apr 03 '11 at 17:44
  • @Matthew. Yes, it does now. I'm slow with all this handlers (new to Java). However, IDE complained at @Override and I don't think dialog.dismiss() needed. I finish activity anyway, so child dialog will be distroyed anyway, correct? When I run it works properly both ways, I'm just not sure if I need those 2 lines – katit Apr 03 '11 at 22:09
  • Generally using @Override is recommended to prevent spelling errors. However I believe @Override for interfaces (like the code above) is only present in Java 6 and above. – Matthew Apr 04 '11 at 01:17
  • @Matthew. Hah, another issue :) Obviously, onDismiss get's called when screen rotated. So, once I rotate screen while login Dialog displayed - App just shuts down. Is there any way to handle that properly? Or at least prevent screen rotation for Dialog only, not for whole app? – katit Apr 04 '11 at 04:08
  • I found how I can prevent orientation change. Thanks! – katit Apr 04 '11 at 04:15