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.