How to keep FTP connection (or any connection obje

2019-06-04 19:04发布

问题:

I'm coding a very basic FTP client on top of my application and I have 2 activities. The first one is the file explorer and the second one is the image viewer. Once I click on the image filename on the explorer, I want to pass the connection to the other activity to handle extra stuff. Basically, I want to keep the same org.apache.commons.net.ftp.FTPClient object (which handles the connection) alive in-between the 2 activities. I know I can't pass an object inside an intent so I don't know what my best bets are.

Thanks.

回答1:

Extend yout application class and code the ftp connection there, every time you need to use it call the Application

public class FtpConnect extends Application {

//FTP CODE

}

where ever you need to use it

FtpConnect singleFtpHandler = (FtpConnect) CurrentActivityClass.this.getApplication();              
singleFtpHandler.method();

You need to add the appExtender name to your manifest like this:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".FtpConnect">