简体   繁体   English

检测活动何时更改

[英]Detecting when activity changes

I am trying to add a save dialog to an Activity . 我正在尝试向Activity添加一个保存对话框。

If you press back, I already have captured it and added a save dialog box. 如果按回去,我已经捕获了它并添加了一个保存对话框。

As I have a generic action bar, is there a way to capture the change of activity event without hardcoding it into the activity change itself? 由于我有通用的操作栏,是否有一种方法可以捕获活动事件的更改而无需将其硬编码为活动更改本身?

I Googled it a bit and found no luck, onDestroy and onStop don't seem to do what I want. 我用谷歌搜索了一下,发现没有运气, onDestroyonStop似乎没有做我想要的。

If you want to know whenever the Activity ceases to become visible (for whatever reason), override onPause (see http://developer.android.com/reference/android/app/Activity.html ). 如果您想知道“活动”何时停止变得可见(无论出于何种原因),请覆盖onPause(请参阅http://developer.android.com/reference/android/app/Activity.html )。

As an aside, as a user, I would want this behaviour - if I move away from an Activity without pressing the back button or quit, I am hoping that it will handle everything silently - ie you use onPause (or alternatives) to store things so that when the activity resumes it has everything as I left it. 顺便说一句,作为用户,我会想要这种行为-如果我离开了一个Activity而没有按下后退按钮或退出,我希望它可以静默处理所有事情-即您使用onPause(或替代方法)存储东西这样,当活动恢复时,便拥有了我离开时的一切。

One way you could do this would be write a class which inherits from Activity and implement the behavior in that class. 实现此目的的一种方法是编写一个从Activity继承并实现该类中行为的类。 Then simply inherit this class for all your activities. 然后只需为所有活动继承此类。

You could write one class which implements the save behaviour 您可以编写一个实现保存行为的类

class SaveActivity extends Activity {

    //...

    @Override
    public void onBackPressed() {
        // common behaviour you want
    }
}

Then in your activities you could do this 然后,在您的活动中,您可以执行此操作

class MyActivity extends SaveActivity {

    // code for this activity

}

onPause() might be what you should be looking at. onPause()可能是您应该查看的内容。 It is called when any activity comes onto the top of this Activity. 当任何活动进入该活动的顶部时,将调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM