What is the best way to create accessible methods for any activity?

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 that this method I created needs to be called in all other activities. Would it 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 like this:

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

Then? Is the way I'm doing correct? Or is there some other way and has some advantage that I still do not know.

    
asked by anonymous 22.04.2014 / 07:01

1 answer

8

The possible ways to do (which occur to me) are:

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

Advantage: If putting the method into a SuperActivity is a design decision that makes sense, the method will be immediately available to you in your other activities, without instantiating a class that contains the said method;

Disadvantage: You will be required to have SuperActivity as the superclass of all Activities that require this method, which makes the code brittle . So this way of providing the method should only be used when it makes sense in your class design.

2) Make the static method and call it without instantiating the class:

SuaClasse.metodo();

Advantage: Exceptions to class instantiation.

Disadvantage: The class runs the risk of becoming a generic class Utilidades , medium "lost" in the middle of the code and displaying sparse and unrelated features. >

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

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

Disadvantages: Same as a class with static methods, with the added disadvantage of running memory leaks by leaking a context other than the global context getApplicationContext() ). Therefore, always try to pass the global context in the class constructor whenever possible.

Possible variations of this latter way include creating a Singleton of the class in question or making it a subclass of Application , declaring it in AndroidManifest.xml . Again, these forms of implementation should be adopted if they make sense in your design.

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

    
22.04.2014 / 13:05