passing variable data to non-activity classes

后端 未结 2 545
渐次进展
渐次进展 2021-01-29 06:43

What is the best way to pass variable data from an Activty to a non-Activty class? The data is created within the onCreate of the first

2条回答
  •  抹茶落季
    2021-01-29 07:15

    If your Class Name is MainClass then Create Constructor for that and pass value as a parameter, below is a example for that.

    MainClass.java

    public class MainClass {
        public MainClass(int i, String str) {
            // TODO Auto-generated constructor stub
    
        }
    }
    

    Use below code for Pass Value from MainActivity to MainClass, Here i is integer variable and str is a String variable

    MainClass mMainClass = new MainClass(i, str);
    

提交回复
热议问题