Passing a method from another class

后端 未结 4 511
渐次进展
渐次进展 2020-12-22 03:09

I want to call a method from one class to another, don\'t know if that\'s possible without extends the class that contains the method. I\'ve tried to import the package, tha

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 04:03

    If you want to call the method without creating an instance of the class then make it static:

    Class1
    {
    ...
        public static void method1() {doSomething;}
    }
    
    Class2
    {
        public class2()
        {
            Class1.method1(); //should be fine
        }
    }
    

    edit: If you want to pass data between Activities that is slightly different. Generally you do something like startActivityForResult() and then retrieve the return value, or use Intents like so:

    How do I pass data between Activities in Android application?

提交回复
热议问题