user5174952
user5174952

Reputation: 147

Android Use OnClick Method From Another Class

I have a class that has its own activity to upload a file onto an FTP server by clicking on a button, this works fine. I have another class that also has a button which renames the uploaded files in my main activity. I would like to 1) upload file then 2) rename, this I can accomplish by clicking on the two buttons from these two separate activities in the right sequence, I would like to use ONE button from my main activity to call the upload method from class A (and not switch activity) and then run the rename method, below is the code from class A which I would like to run from class B button onclick.

ClassA:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ftpupload);

    //System.out.println("dirClass.dirFtpSignature :" +dirClass.dirFtpSignature);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
      }

    btn = (Button) findViewById(R.id.btnUpload);
    btn.setOnClickListener(this);

}

public void onClick(View v) {


    /********** Pick file from sdcard *******/
    File f_sender = new File(dirClass.dirSignature + "image.png");

    // Upload sdcard file
    uploadFile_sender(f_sender);

}

public void uploadFile_sender(File fileName){

    System.out.println("Connecting to FTP...");

Upvotes: 0

Views: 187

Answers (1)

user5174952
user5174952

Reputation: 147

I got it,

On Class B I declared Class A and then used the onClick method of Class A in Class B. (I just had to comment out Toasts in Class A because they are unknown to Class B, but I think this can also be accomplished if I make them public static too)

Anyway here is the code used below:

    public class MyClassB extends MainActivity {


    FtpUpload fu = new FtpUpload(); //MyClassA

    ......  


    public void onClick(View v) {


                    runOnUiThread(new Runnable() {
                          @Override
                          public void run() {   

                            fu.f_sender = new File(dirClass.dirSignature + "image.png");

                            fu.uploadFile_sender(fu.f_sender);

Upvotes: 1

Related Questions