What is the best way to create methods accessible to any Ctivity?

Asked

Viewed 3,020 times

5

Hello. When I need to create a method for an Activity I simply write it in my Activity code. For example:

package com.pcriot.maxsoft.testapplication;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void MeuMetodo() {
        // Código aqui
    }
}

But now, let’s say this method that I created needs to be called in all other activities. It would be advisable to create a class to write the methods that would be used by the activities?

A class like this:

package com.pcriot.maxsoft.testapplication;

import android.content.Context;

public class Functions {
    private Context context = null;

    public Functions(Context context) {
        this.context = context;
    }

    public void MeuMetodo1() {
        // Código aqui
    }

    public void MeuMetodo2() {
        // Código aqui
    }

    public void MeuMetodo3() {
        // Código aqui
    }
}

Then I could call each of them so:

package com.pcriot.maxsoft.testapplication;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
    private Functions Functions = new Functions(MainActivity.this);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Functions.MeuMetodo1();
    }
}

So? Is the way I’m doing it right? Or is there some other way and that has some advantage that I don’t yet know.

  • I think you’re doing well, I don’t see any other way to do it.

1 answer

8


The possible ways of doing (which occurs to me) are:

1) Create a Superactivity that other Activities extend and therefore inherit the method in question:

Upside: If placing the method in a Superactivity is a design decision that makes sense, the method will already be immediately available to you in your other activities, without needing to instantiate a class that contains the said method;

Downside: You will be required to have Superactivity as a superclass of all Activities that need this method, which makes the code more Brittle (cast). Therefore this way of providing the method should only be used when it makes sense in your class design.

2) Make the method static and call it without needing to instantiate the class:

SuaClasse.metodo();

Upside: Dispenses with the instantiation of the class.

Downside: The class risks becoming a generic class Utilidades, "lost" medium in the middle of the code and presenting sparse and unrelated functionalities.

3) Instantiate the class and call the method in question:

SuaClasse objeto = new SuaClasse(contexto);
objeto.metodo();

Disadvantages: The same as in a class with static methods, with the additional disadvantage of running the risk of memory Leaks by leaking a context other than the global context (getApplicationContext()). So look whenever possible for the global context in the class constructor.

Possible variations of the latter include creating a Singleton or make it a subclass of Application, declaring it in the AndroidManifest.xml. Again, these ways of implementing should be adopted if they make sense in your design.

I do not think I have mentioned all the advantages and disadvantages, improvements are welcome.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.