安卓:要去&QUOT时保存应用程序状态,背景"

问题描述:

我有一个体面的应用程序状态,需要持久化(特定格式)一个复杂的Andr​​oid应用程序。写出数据不是瞬时操作,因此它是理想的,以减少不必要的持续它

I have a complex Android app with a decent amount of application state that needs to be persisted (in a specific format). Writing out the data is not an instantaneous operation and so it would be ideal to minimize persisting it unnecessarily.

的持续存在由于开销是不实际每次改变时坚持的状态。理想的情况是,当应用程序是中背景通过点击家按钮或点击返回按钮上的应用程序的根活动(或来电等)用户持续状态将被触发。这最大限度地减少持久性开销,同时保持从用户的角度来看,状态的一致性。现在的问题是你怎么能检测如果应用程序正在中背景?

Because of the persistence overhead it is not practical to persist the state every time it is changed. Ideally, persisting state would be triggered when the app is "backgrounded" by the user tapping the 'home' button or tapping the 'back' button on the app's root Activity (or by an incoming call, etc.). This minimizes persistence overhead while maintaining state consistency from the user's perspective. The question is how can you detect if the app is being "backgrounded"?

该活动的生命周期电话(ONSTART,onResume,在onPause和朋友)不帮的应用程序有许多不同的活动,其中的任何一个都可能被激活,当用户点击家。此外,该电话被调用时,活动得到推动,弹出(和底部杀死)在活动栈,所以他们不反映,如果该应用程序会消失或没有。

The Activity lifecycle calls (onStart, onResume, onPause and friends) don't help as the app has many different activities, any one of which could be active when the user taps 'home'. Furthermore, the calls get called when Activities get pushed, popped (and bottom killed) on the Activity stack and so they don't reflect if the app is going away or not.

那么,如何一个应用程序检测时,它会在后台?

So how does an app detect when it is going to the background?

如果你想坚持一些状态时,任何你的活动转到后台,你总是可以延长活动,增加你的在onPause和onResume调用两个abstact方法。然后,你的活动每一个延伸这个新的抽象类将*定义saveState和()和LoadState()。这些方法可以定义什么保存和载入的各项活动。

If you want to persist some state when any of your activities goes to the background you could always extend Activity, add two abstact methods which you call in onPause and onResume. Then each one of your Activities which extends this new abstract class would be forced to define saveState() and loadState(). These methods could define what to save and load for each activity.

这是使用继承,迫使你的程序员来实现,否则忽视的方法和技巧方法只是一个例子。你可以告诉你的程序员,如果你需要保存一个活动的状态,只是延长这种类型的活动,然后在IDE就会把他们设计的道路。

That is just an example of using inheritance to force your programmers to implement otherwise overlooked methods and techniques methods. You can just tell your programmers, if you ever need to save the state of an activity just extend this type of activity and then the IDE will put them on the path of your design.

package com.yourcompany.yourpackage;

import android.app.Activity;

public abstract class ActivitySaveState extends Activity{

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadState();
    }


    public abstract void loadState();
    public abstract void saveState();



}

你也可以实例一些国家储蓄它们机制的超类(即Web服务端点,DAO或W / E您的持久性单元可能。

Also you could instantiate some of the state saving mechanisms for them in the super class (i.e. web service endpoint, DAO or w/e your persistence unit maybe.

@Override
    protected void onResume() {
        super.onResume();
        saveState();

        CustomDataAccessObject dao = new CustomDataAccessObject("Activity3");

        loadState(dao );
    }


    public abstract void loadState(CustomDataAccessObject dao);