How to trigger onPause programmatically in android activity

本秂侑毒 提交于 2019-12-01 08:03:50

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.

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)

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

1) define a method.

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

2) call method.

callLifeCycleMethod().callActivityOnDestroy(MainActivity.this);

or

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