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.
– Jorge B.