3

I have tried below code to check Activity's call backs, As of my understanding whenever dialog comes on top of Activity, Activity's OnPause() method should call. When dialog disappears Activity's OnResume() should trigger.

Surprisingly it's not happening the way it should work.I have tested on Android M 6.0.1

Please correct me if I am wrong, Here is my code,

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onPause() {
        Log.d("MainActivity", "onPause");
        super.onPause();
    }

    @Override
    protected void onResume() {
        Log.d("MainActivity", "onResume");
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    public void onClick(View view) {
        AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(MainActivity.this);
        }
        builder.setTitle("Delete entry")
                .setMessage("Are you sure you want to delete this entry?")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with delete
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }
}  
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Anil Raavi
  • 131
  • 1
  • 10

2 Answers2

10

I have tried below code to check Activity's call backs, As of my understanding whenever dialog comes on top of Activity, Activity's OnPause() method should call. When dialog disappears Activity's OnResume() should trigger.

NO. Documentation is bit confusing. When you call a dialog on top of activity, onPause() of activity will never call. Think it in other way, dialog always tied-up with calling activity, and if that activity in not in running state, how that dialog would be visible?

Actually, onPause() will only call when you are calling another activity as dialog (using dialog theme, or an activity which does not cover full screen).


From documentation

onPause()

  • A new, semi-transparent activity (such as a dialog) opens. As long as the activity is still partially visible but not in focus, it remains paused.

You can also read Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
7

onPause() is called when your activity is no longer on top of activity stack. A Dialog by itself is not an activity, so will not replace the current activity at the top of the stack, so onPause() is never called.

However if you implement Dialog as an Activity : (An activity which has a theme set to that of a dialog). In this case when displaying dialog as an activity will cause the new activity to be on top of the stack, hence calling onPause()

Ayush Khare
  • 1,802
  • 1
  • 15
  • 26