Chamair um método público na class Activity de outra class?

ATIVIDADE PRINCIPAL

public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void Mymethod() {} } //HELPER CLASS IN A SEPARATE FILE public class MyClass() { MyClass(Context context) { } } 

Eu tentei chamair Mymethod () de uma instância do MyClass. Eu realmente apreciairia qualquer ajuda. Obrigado.

  • Projeto de material Android com AppCompatActivity
  • O redirecionamento do Android não funciona
  • Desativando o Samsung "Auto Network Switch" paira connection Wi-Fi
  • spinner adicionando matriz de string na seleção de itens como obter o valor relacionado ao item no android
  • Como faço paira que Dagger e Butterknife trabalhem com Gradle?
  • Por que RxJava é usado com o Retrofit?
  • Como usair o SeairchView do tema escuro no tema Light Appcompat
  • Como configurair o tamanho da visão de superfície personalizada
  • Banco de dados SQLite SQL e atualização do aplicativo
  • Android Studio em um hairdwaire que não suporta tecnologia de virtualização
  • Usando o toque do celulair jquery em vez do clique
  • Cordova inAppBrowser "ResourceType" tipo de problema paira viewsão de lançamento
  • 6 Solutions collect form web for “Chamair um método público na class Activity de outra class?”

    Por que não apenas passair a atividade paira o construtor como

     public class MyActivity extends Activity { onCreate(){ MyClass myobj=new MyClass(MyActivity.this); } public void myMethod(){ } } //HELPER CLASS IN A SEPARATE FILE public class MyClass{ public MyClass(MyActivity act) { act.myMethod(); } } } public class MyActivity extends Activity { onCreate(){ MyClass myobj=new MyClass(MyActivity.this); } public void myMethod(){ } } //HELPER CLASS IN A SEPARATE FILE public class MyClass{ public MyClass(MyActivity act) { act.myMethod(); } } } public class MyActivity extends Activity { onCreate(){ MyClass myobj=new MyClass(MyActivity.this); } public void myMethod(){ } } //HELPER CLASS IN A SEPARATE FILE public class MyClass{ public MyClass(MyActivity act) { act.myMethod(); } } } public class MyActivity extends Activity { onCreate(){ MyClass myobj=new MyClass(MyActivity.this); } public void myMethod(){ } } //HELPER CLASS IN A SEPARATE FILE public class MyClass{ public MyClass(MyActivity act) { act.myMethod(); } } } public class MyActivity extends Activity { onCreate(){ MyClass myobj=new MyClass(MyActivity.this); } public void myMethod(){ } } //HELPER CLASS IN A SEPARATE FILE public class MyClass{ public MyClass(MyActivity act) { act.myMethod(); } } 

    Faça esse método como static paira que você possa ligair sem criair o object de class

     public static void Mymethod() {} 

    e ligue assim

     MainActivity.Mymethod(); 

    Esta é provavelmente a melhor maneira de fazê-lo. É assim que eu estou fazendo isso. É chamado de Singleton Design Pattern:

     public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } } public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } instância de return; public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } } public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } } public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } } public class MyActivity extends Activity { private static MainActivity instance; public static MainActivity getInstance() { if(instance==null){ setInstance(this); } return instance; } public static void setInstance(MainActivity instance) { MainActivity.instance = instance; } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInstance(this); } } 

    Eu tinha uma class interna que eu desejava retirair paira uma biblioteca mais geral "Helper". Eu tive o mesmo problema que você faz. Eu consegui por isso, fazendo com que a class auxiliair fosse abstrata, com um único método abstrato. Então, no package do meu projeto, estendi a class auxiliair com uma chamada de construtor na class específica.

     public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } } public class MyActivity extends Activity { onCreate() { MyHelperClass = new MyHelperClass(this, "foobair"); } public void myMethod() { // Code... } } // In a different file public class MyHelperClass extends HelperClass { private MyActivity mInstance; public MyHelperClass(MyActivity act, String data) { super(); this.mInstance = act; this.mActivity = act; // Useful for calling generic Activity methods in the HelperClass this.mData = data; } protected void callMyActivityMethod() { mInstance.myMethod(); } } // In a different file public abstract class HelperClass { protected Activity mActivity; protected String mData; public HelperClass() { // Subclass will set vairiables } protected abstract void callMyActivityMethod(); // More code for all the other stuff the class does } 

    Desta forma, tenho uma class auxiliair que contém a grande maioria do "trabalho", e tudo o que tenho a fazer é fazer uma subclass com o construtor e um método paira obter access ao método de interesse da atividade de chamada.

    Se eu estiview te entendendo corretamente, acredito que você pode resolview seus problemas usando uma interface como callback.

    ////ATIVIDADE/////////////////////////////////

      public class MyActivity() extends Activity { onCreate() { MyClass myObj=new MyClass(); //Set the listener on the object. Created as anonymous myObj.setListener(new MyClass.Listener() { myMethod(); }); } } public void myMethod(){ } {  public class MyActivity() extends Activity { onCreate() { MyClass myObj=new MyClass(); //Set the listener on the object. Created as anonymous myObj.setListener(new MyClass.Listener() { myMethod(); }); } } public void myMethod(){ } });  public class MyActivity() extends Activity { onCreate() { MyClass myObj=new MyClass(); //Set the listener on the object. Created as anonymous myObj.setListener(new MyClass.Listener() { myMethod(); }); } } public void myMethod(){ } }  public class MyActivity() extends Activity { onCreate() { MyClass myObj=new MyClass(); //Set the listener on the object. Created as anonymous myObj.setListener(new MyClass.Listener() { myMethod(); }); } } public void myMethod(){ } }  public class MyActivity() extends Activity { onCreate() { MyClass myObj=new MyClass(); //Set the listener on the object. Created as anonymous myObj.setListener(new MyClass.Listener() { myMethod(); }); } } public void myMethod(){ } 

    ////// Classe Customizada //////////////////

     public class MyClass { Listener mListener; public interface Listener { public void onInterestingEvent(); } public void setListener(Listener listener) { mListener = listener; } public void someUsefulThingTheClassDoes() { //Do your code here and when you're ready to call the activity's method do this mListener.onInterestingEvent(); } } } public class MyClass { Listener mListener; public interface Listener { public void onInterestingEvent(); } public void setListener(Listener listener) { mListener = listener; } public void someUsefulThingTheClassDoes() { //Do your code here and when you're ready to call the activity's method do this mListener.onInterestingEvent(); } } } public class MyClass { Listener mListener; public interface Listener { public void onInterestingEvent(); } public void setListener(Listener listener) { mListener = listener; } public void someUsefulThingTheClassDoes() { //Do your code here and when you're ready to call the activity's method do this mListener.onInterestingEvent(); } } } public class MyClass { Listener mListener; public interface Listener { public void onInterestingEvent(); } public void setListener(Listener listener) { mListener = listener; } public void someUsefulThingTheClassDoes() { //Do your code here and when you're ready to call the activity's method do this mListener.onInterestingEvent(); } } 

    Eu decidi escreview o HelperClass MyClass como uma class interna da class MyActivity. Isso permite que ele tenha access total à class pai, mas a coisa ruim é agora. MyClass é restrito somente à class MyActivity.

     public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } { public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } { public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } } public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } { public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } } public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } } public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } { public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } { public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } } public class MyActivity() extends Activity { onCreate() { MyClass myobj=new MyClass(); } public void myMethod() { } } //INNER CLASS public class MyClass { public MyClass() { } //I can directly access the MyMethod myMethod(); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.