2

I am aware that, since AlertDialog is part of a presenting Activity, showing it doesn't trigger OnPause of the Activity. What I want to achieve is to show AlertDialog and somehow trigger OnPause of the presenting Activity.

In the OnResume I set OnGlobalLayoutListener to adjust the position of the scrollview based on whether the keyboard is visible. However, if an AlertDialog is visible then it probably doesn't make sense to keep listening to the layout changes and adjusting the scrollview accordingly, considering the "focus" is currently on the AlertDialog and the Activity is partially visible in background.

I am aware that I can start activity with "popup" style but this is not what I want; I want to use AlertDialog.

Asad
  • 75
  • 1
  • 9
  • 2
    Why not you are using on Dismiss Listener of AlertDialog. Playing with Activity's onPause is the worst idea, and keep in mind that if you call, your AlertDialog will also be dissmissed. – Pankaj Kumar Jan 11 '19 at 04:05

1 Answers1

0

Use another activity as dialog. Then it will automatically pause your current activity.To start activity as dialog I defined it like this in AndroidManifest.xml:

<activity android:theme="@android:style/Theme.Dialog" />

Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list

android:excludeFromRecents="true"

If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:

After setContentView() in your Activity use:

this.setFinishOnTouchOutside(false);

Now when I call startActivity() it displays as a dialog, with the previous activity shown when the user presses the back button.

Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use @style/Theme.AppCompat.Dialog instead.

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40