How can I manually trigger my Android onPause() method to test it?

2.6k views Asked by At

I would like to manually test that the Android app I am writing works correctly when paused. What can I do through the Android GUI to cause the onPause() method to be called without causing onStop() to also be called?

I've tried pulling down the notification bar and receiving a phone call, but neither causes onPause() to be called.

I would like to find a non-programatic solution that works for multiple versions of Android through the GUI, with or without phones, because I am teaching a class full of students using different devices.

Note that I am not asking how to tell if my onPause() method is called. I am asking what I can to do cause it to be called, without modifying my application.

6

There are 6 answers

0
Ellen Spertus On BEST ANSWER

I found a solution. I got the idea from a picture in the book Head-First Android Development.

  1. Install Any Do.
  2. Create a task.
  3. Set an alarm for a few minutes in the future.
  4. Start the app under test.
  5. When the alert comes up, my app gets partly obscured, and its onPause() method gets called. If I click on the Dismiss icon, my app resumes without its onStop() method having been called.

Picture showing app partly obscured by Any Do alert

2
omriherman On

You could call it programmatically.

Or simply start with intent a different activity.

4
santosh kumar On

This can be one way of doing it in application.

  @Override
    protected void onPause () {
        super.onPause();
        Toast.makeText(this, "Paused", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    protected void onStop () {
        super.onStop();
        Toast.makeText(this, "stopped", Toast.LENGTH_SHORT).show();
    }

Other way you have is to open any Activity/Application using intent.

4
Akanksha Hegde On

According to the documentation paused state occurs, if an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

Hence if you attend a call obviously your call screen will come at the stack and it will call onStop() of the previously running activity.

So if you want only onPause() to be called you should open a new non-full-sized or transparent activity.

2
sats On

You can create an alert dialog to call the onPause() without calling onStop().

But the important point to note is to create the alert dialog as an activty.

This can be done as follows:

Create a new activty class called dialog extending AppCompatActivty, and also add it to AndroidManifest.xml as shown:

public class dialog extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set appropriate layout
        setContentView(R.layout.activity_main);
    }
}

Add to AndroidManifest.xml:

<application>
....
    <activity
                android:name="dialog"
                android:theme="@style/Theme.AppCompat.Dialog"></activity>
</application>

Add the following to onCreate() method of MainActivty.java:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), dialog.class);
                startActivity(intent);
            }
        });
0
manwtein On

Actually in 29 API notification may not work.
For manually calling methods onPause()/onResume() should hold the home-button