How to call method in main activity from other activity?

前端 未结 5 1948
清歌不尽
清歌不尽 2020-12-20 07:25

I want to call public method in main activity from other activity. How can I do that?

class MainActivity extends Activity {
    public void myMethod() {}
}

         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 08:01

    If you to have static methods to call from any activity you should have a an Utililty or a Helper Class where you can call the methods staticly from anywhere I don't think that its a good pratice to bee caling static methods on one activyty to another

    Here is an Example of an Helper Class

       public Class ActivityHelper{
    
        public static void myMethod(Context context){
        // If you need to do something with your Context
    
        }
    
    /* and you can create a lot of  static methods that you would need to use from any activity or service on your app*/
    
    }
    

提交回复
热议问题