How to trigger onPause programmatically in android activity

断了今生、忘了曾经 提交于 2019-12-01 06:22:01

问题


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.


回答1:


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.




回答2:


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)




回答3:


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




回答4:


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




回答5:


1) define a method.

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

2) call method.

callLifeCycleMethod().callActivityOnDestroy(MainActivity.this);

or

callLifeCycleMethod().callActivityOnPause(MainActivity.this);


来源:https://stackoverflow.com/questions/17579941/how-to-trigger-onpause-programmatically-in-android-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!