user2022581 user2022581 - 8 months ago 72
Android Question

How to trigger onPause programmatically in android activity

I am trying to work out how I can simulate pausing an activity for debugging my app. I want onPause to be called but NOT onStop. I just want to try a pause resume cycle and am looking for some code i can call (e.g. after a button press) to trigger this.

Any ideas how?

I have seen people suggest pressing the home button in other threads but when I do this is stops the app and calls onStop as well as onPause so it isn't quite what I was looking for.

Answer Source

Taken from this link: The easiest is to add a semitransparent activity on top of your activity. I did the test myself and onStop is not called indeed:

The transparent activity:

public class TransparentActivity extends FragmentActivity {
    protected void onCreate(Bundle arg0) {

any simple layout can be used for transparent_layout, but the tricky part is in the Manifest:

            android:theme="@style/Theme.Transparent" >

where in styles.xml:

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>

Then in starter activity:

public class MainActivity extends FragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        findViewById( OnClickListener() {

            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TransparentActivity.class));

    protected void onPause() {
        Log.d("TSTAct", "#onPause");

    protected void onStop() {
        Log.d("TSTAct", "#onStop");

    protected void onResume() {
        Log.d("TSTAct", "#onResume");

    protected void onStart() {
        Log.d("TSTAct", "#onStart");

When opening the TransparentActivity I can see in the Logcat only:

07-10 23:35:28.323: D/TSTAct(27180): #onPause

no onStop call.