Question

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.

Was it helpful?

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.

OTHER TIPS

Simple way, From Mainactivity i will call another activity using Intent. In Manifest i will define as

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

Means for another activity i will add style as "Theme.AppCompat.Dialog" means this will looks as Dialog.

From Main activity if you call this using Intent , then "AnotherActivity" will show as Dialog , It will come in on top of Main activity, that time main activity will be in onPause state (It will not call onStop state of MainActivity)

1) define a method.

public static Instrumentation callLifeCycleMethod()
{
   return new Instrumentation();
}

2) call method.

callLifeCycleMethod().callActivityOnDestroy(MainActivity.this);

or

callLifeCycleMethod().callActivityOnPause(MainActivity.this);

Another way to debug what happens with onPause and onResume is to make use of the Android test framework. In an activity test you can get an Instrumentation object and then trigger pause and resume:

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;

public class LaunchActivityTest extends ActivityInstrumentationTestCase2<LaunchActivity> {

    private LaunchActivity launchActivity;

    public LaunchActivityTest() {
        super(LaunchActivity.class);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void setUp() throws Exception {

        super.setUp();
        launchActivity = getActivity();
    }

    @Override
    protected void tearDown() throws Exception {

        super.tearDown();
    }

    public void testPause() {

        Instrumentation ins = getInstrumentation();
        ins.callActivityOnPause(launchActivity);
        assertEquals(true, true);
        ins.callActivityOnResume(launchActivity);
    }

}

Note that I am just using a dummy assert here. As an aside I am not yet sure how i can assert that the methods were called in Java

Another option might be to call the private method performPause in Activity using reflection. Something like this should work in principle:

Method method = myactivity.getClass().getSuperclass().getDeclaredMethod("performPause");
method.setAccessible(true);
method.invoke(myactivity);

Note I have yet to test it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top