2

I have an app with several activities and the action to perform on destruction is actually the same, no matter what the Activity is at that moment (deleting some data and performing a HTTP call).
Is there a way to have all activities share the onDestroy method?

Another solution would be to factorize a method in some other static class and have each Activity invoke it during destruction, but a trait-like behaviour would be actually nicer.

gioaudino
  • 573
  • 8
  • 23
  • One way is you can use ActivityLifecycleCallbacks . Register that in your application class and then perform the task there. To register the callbacks you can see this answer : https://stackoverflow.com/questions/47690661/how-to-check-if-an-activity-is-in-foreground-background-or-inactive/47691421#47691421 – Saurabh Kataria Dec 11 '17 at 10:48

4 Answers4

2

Create an abstract BaseActivity, override the onDestroy, and all your other activities should just extend the BaseActivity?

Why go with singletons, statics, factories etc if what you are describing is a classic OOP trait? :)

JanBo
  • 2,925
  • 3
  • 23
  • 32
  • It's so easy to let Android Studio create classes that I didn't want to change the signature every time :D – gioaudino Dec 11 '17 at 11:20
  • Yea but even if you use the Android Studio to create a new activity wizard you can chose the parent activity from a dropdown :) – JanBo Dec 11 '17 at 12:52
1

From you description, I understood that you want a common onDestroy() method which will be call by all the activities when calling onDestroy().

Here is the code:

package com.demo.application;

import android.app.Activity;
import android.app.Application;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class AppGlobal extends Application {


    @Override
    public void onCreate() {
        super.onCreate();

        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

    } //End of onCreate
}

You need to add a property within application tag of your manifest.xml file: android:name="com.demo.application.AppGlobal"

Try and let me know your experience.

Hari N Jha
  • 484
  • 1
  • 3
  • 11
0

Use singleton class and create static method in which do what u want to do and in onDestroy call this method.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
0

Easiest way for this is creating an Application class and registering for the life cycle callbacks.

public class MyApp extends Application {
    @Override
    public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

            //Perform common action here
        }
    });
  }
}

Add the Application class in the manifest.

<application
    android:allowBackup="true"
    android:name=".MyApp"
    android:icon="@mipmap/ic_launcher"
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20