Android: saving the state of the application when switching to the "background"

I have a complex Android application with a decent amount of application states that I need to save (in a specific format). Recording data is not an instantaneous operation, and therefore it would be ideal to minimize its unjustified use.

Due to constant overhead, it is not always necessary to maintain a state every time it changes. Ideally, the persisting state will be triggered when the application is “dependent” on the user by pressing the home button or pressing the back button in the application’s root application (or by an incoming call, etc.). This minimizes overhead while maintaining state consistency from the user's point of view. The question is, how can you determine if an application is associated with a "background"?

Activity lifecycle modes (onStart, onResume, onPause and friends) do not help, because the application has many different actions, any of which can be active when the user clicks "home". In addition, calls are called when actions are activated, pushed (and the bottom is killed) onto the Activity stack, and therefore they do not reflect whether the application leaves or not.

So how does an application detect when it goes to the background?

+5
source share
5 answers

- , - , Activity, abstact, onPause onResume. , , saveState() loadState(). , .

, , . , , , IDE .

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();



}

(, -, DAO .

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

        CustomDataAccessObject dao = new CustomDataAccessObject("Activity3");

        loadState(dao );
    }


    public abstract void loadState(CustomDataAccessObject dao);
+4

, Android , . //... . , , , , (?) , .

, :

MyXxxActivity.onPause - , , "" . , , DB/File System, .

, .

, onPause. - (, ), onPause. onPause.

, (UI). . , , , , ( AsyncTask, .).

+2

The Activity lifecycle calls (onStart, onResume, onPause and friends) don't help as the app has many different activities

, , . , , .

0

. android.

, , .


http://developer.android.com/reference/android/app/Activity.html

onPause() - , , . , , , ( ContentProvider, ).

0

, , onSaveInstanceState , . , , onrestore ...

-1

All Articles