如何以编程方式引发的onPause在android系统活动 [英] How to trigger onPause programmatically in android activity

查看:366
本文介绍了如何以编程方式引发的onPause在android系统活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我怎么能模拟暂停活动进行调试我的应用程序。我想的onPause被称作但不是的onStop。我只是想尝试暂停恢复周期,并正在寻找一些$ C C I可以调用(如按钮preSS后)来触发此。

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.

任何想法如何?

我看到有人建议pressing其他线程的home键,但是当我这样做是停止的应用程序,并调用的onStop以及在onPause所以不完全是我一直在寻找。

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.

推荐答案

此的链接:最简单的是你的活动上添加一个半透明的活动。我做了测试我和的onStop 不确实叫:

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:

透明的活动:

public class TransparentActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.transparent_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>

其中styles.xml:

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");
    }
}

在打开的 TransparentActivity 我可以在LogCat中看到的只有:

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

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

没有的onStop 电话。

这篇关于如何以编程方式引发的onPause在android系统活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆