passing variable data to non-activity classes

后端 未结 2 544
渐次进展
渐次进展 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);
    
    0 讨论(0)
  • 2021-01-29 07:15

    Create a class object in that activity class.

    MyClass mc = new MyClass();
    mc.yourVariable = "Your Value";
    
    0 讨论(0)
提交回复
热议问题