DormeoES
DormeoES

Reputation: 101

Detecting when activity changes

I am trying to add a save dialog to an 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.

Upvotes: 0

Views: 97

Answers (3)

nithinreddy
nithinreddy

Reputation: 6197

onPause() might be what you should be looking at. It is called when any activity comes onto the top of this Activity.

Upvotes: 1

Neil Townsend
Neil Townsend

Reputation: 6084

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).

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.

Upvotes: 2

Ahmed Aeon Axan
Ahmed Aeon Axan

Reputation: 2139

One way you could do this would be write a class which inherits from Activity and implement the behavior in that class. 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

}

Upvotes: 1

Related Questions