Call a public method in the Activity class from another class?

前端 未结 8 2179
无人及你
无人及你 2020-12-05 19:33

MAIN ACTIVITY

    public class MyActivity() extends Activity
    {
        onCreate()
        {
            MyClass myobj=new MyClass();    
        }
               


        
相关标签:
8条回答
  • 2020-12-05 19:57

    If I'm understanding you correctly I believe you can solve your problems using an interface as a callback.

    ////ACTIVITY/////////////////////////////////

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

    //////Custom Class//////////////////

    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();
         }
    }
    
    0 讨论(0)
  • 2020-12-05 19:59

    Make that method as static so you can call without creating the class object

    public static void Mymethod()
    {}
    

    and call like this way

    MainActivity.Mymethod();
    
    0 讨论(0)
  • 2020-12-05 19:59

    In MainActivity.class file You have to pass MainActivity context from MainActivity Class. Then in MyClass you have to Get MainActivity context. Remember Context and MyActivity are two different reference.

    public class MyActivity extends Activity
    {
        onCreate(){
            MyClass myobj=new MyClass(MyActivity context);    
        }
        public void Mymethod(){}
    }
    

    //HELPER CLASS IN A SEPARATE FILE

    public class MyClass()
    {
        MyActivity context;
        MyClass(MyActivity context)
        {
          this.context = context;
    
          this.context.Mymethod();
          //Or you can directly use activity context
          context.Mymethod();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 20:00

    Why not just pass the activity to the constructor like

    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();
        } 
    } 
    
    0 讨论(0)
  • 2020-12-05 20:00

    I decided to write the HelperClass MyClass as an inner class of MyActivity class. This allows it full access to parent class but the bad thing is now MyClass is restricted to MyActivity class only.

    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();
        }
    
    0 讨论(0)
  • 2020-12-05 20:09

    You have to pass instance of MainActivity into another class, then you can call everything public (in MainActivity) from everywhere.

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        // Instance of AnotherClass for future use
        private AnotherClass anotherClass;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // Create new instance of AnotherClass and
            // pass instance of MainActivity by "this"
            anotherClass = new AnotherClass(this);
        }
    
        // Method you want to call from another class
        public void myMethod(){
            ...
        }
    }
    

    AnotherClass.java

    public class AnotherClass {
    
        // Main class instance
        private MainActivity mainActivity;  
    
        // Constructor
        public AnotherClass(MainActivity activity) {
    
            // Save instance of main class for future use
            mainActivity = activity;  
    
            // Call method in MainActivity
            mainActivity.myMethod();
        }
    }
    
    0 讨论(0)
提交回复
热议问题