简体   繁体   中英

Android: Create a new class? Or can I use the current Activity?

Really I guess I am looking for a best practice answer. I like to leave applications with the lightest amount of code possible and the least amount of repeated code especially. I have a few Android applications that I have created. My question is, if I create a new Activity which in turn also needs a class to be created of a similar name, should I create the class separate or is there some way that I can keep the class that extends the Activity already?

Here is an example, lets say I want to create a small activity that is related to Pinging an Address. I could implement Parcelable on that Activity and use my Activity as a data holder Class.

public class PingAddress extends Activity implements Parcelable{
public String Scan;
public String ScanName;
public String IpAddress;
public String Timeout;
public String NumberOfPings;

public PingAddress() {

}
    public void onCreate(Bundle bundle) { 
        super.onCreate(bundle);
    }

What I have been doing is just creating the activity and then creating a separate class, using the example above, called PingAddressData, here is an example of that class.

public class PingAddressData implements Parcelable{
public String Scan;
public String ScanName;
public String IpAddress;
public String Timeout;
public String NumberOfPings;

public PingAddress() {

} 

Again, I am really looking for best practice here with Android. Does anyone know of repercussions that I would run into if I just implemented Parcelable on an Activity? My main objective is to cut down on code pages. I created a separate Package just to store all of the data classes in that I created, but were really a representation of that Activity.

Thanks

I'm not sure about a "best practice", but it is certainly a good idea to keep your Parceable separate than the Activity. If you do make your Activity Parceable and pass it through an Intent you risk keeping references to Activites past and thus open yourself up for memory leaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM