The method of Android common class calling the function in the activity

The method of Android common class calling the function in the activity


Assume MainActivity activity and Student class are defined.
The code of MainActivity is:

public class MainActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public String  getName(){
    
    
        return "Tom";
    }
}

Student's code is:

public class Student {
    
    
    public void fun(){
    
    
        
    }
}

At this point, it is necessary to realize that Student calls the getName() function of MainActivity.

1. Activity name. Function name ()

Since Activity cannot be instantiated with new, it can be called through activity name. function name (). At this time, the called function needs to be modified with the static keyword.
The code of MainActivity is:

public class MainActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public static String  getName(){
    
    
        return "Tom";
    }
}

Student's code is:

public class Student {
    
    
    public void fun(){
    
    
        String name = MainActivity.getName();
    }
}

At this point, Student successfully calls the getName() function of MainActivity.
With this method, you need to pay attention to some problems. Because static modified functions cannot refer to non-static fields, non-static variables, non-static controls, etc. cannot be used in the getName() function. If you want to use them, you need to modify variables and controls with static, but it is better to use less static fields. .
In addition, this method does not support updating UI controls in sub-threads, that is, runOnUiThread() cannot be used in functions decorated with static.

2. Customize a class and call it indirectly through this class

Customize a class that will get the Context and Activity in the activity. Therefore, ordinary classes can indirectly call functions in activities through this class.
The custom class IndirectClass code is:

public class IndirectClass {
    
    
    private Context contxt;
    private MainActivity activity;

    public Context getContxt() {
    
            
        return contxt;
    }

    public void setContxt(Context contxt) {
    
            
        this.contxt = (MainActivity) contxt;
    }

    public Activity getActivity() {
    
    
        return (MainActivity) activity;
    }

    public void setActivity(Activity activity) {
    
    
        this.activity = (MainActivity) activity;
    }

    public IndirectClass(Context context, MainActivity activity) {
    
    
        this.setContxt(context);
        this.setActivity(activity);
    }
}

After definition, you need to declare and initialize the IndirectClass class in MainActivity, and then you can reference it in ordinary classes.
The code of MainActivity is:

public class MainActivity extends AppCompatActivity {
    
    
    public static IndirectClass indirectClass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 对IndirectClass类进行初始化
        indirectClass = new IndirectClass(this,MainActivity.this);
    }
    public String  getName(){
    
    
        return "Tom";
    }
}

Student's code is:

public class Student {
    
    
    public void fun(){
    
    
        // 引用IndirectClass类来调用MainActivity的函数
        IndirectClass indirect= MainActivity.indirectClass;

        Context context= (Context) indirect.getContxt();
        MainActivity activity = (MainActivity) indirect.getActivity();

        String name = activity.getName();
    }
}

At this point, Student successfully calls the getName() function of MainActivity.

Guess you like

Origin blog.csdn.net/qq_34205684/article/details/110822168