Search code examples
androidactivity-lifecycle

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.


Solution

  • 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 {
        @Override
        protected void onCreate(Bundle arg0) {
            super.onCreate(arg0);
            setContentView(R.layout.transparent_layout);
        }
    }
    

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

    <activity
                android:name=".TransparentActivity"
                android:theme="@style/Theme.Transparent" >
            </activity>
    

    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>
    </style>
    

    Then in starter activity:

    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.btnNext).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this, TransparentActivity.class));
                }
            });
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.d("TSTAct", "#onPause");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.d("TSTAct", "#onStop");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.d("TSTAct", "#onResume");
        }
    
        @Override
        protected void onStart() {
            super.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.