Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android object oriented programming

Tags:

java

oop

android

I am fooling around with some basic programming in Android using Eclipse. I'm currently looking through a book and playing with some sample code that is written in the book.

I have noticed that in this particular book, all of the examples so far take pace in Main-Activity. I don't believe this to be very good Object Oriented programming practice as I am from a traditional Java background.

Is this the common practice for mobile platforms? Shouldn't classes all be contained in their own files?

like image 382
JCC Avatar asked Sep 10 '11 21:09

JCC


People also ask

What is object oriented programming in Android?

Learn Object Oriented Concepts for Android: Object-oriented programming (OOP) is a programming language model that allows users to create and organize Java applications on desktops.

Does Android use C++?

C++ C++ can be used for Android App Development using the Android Native Development Kit(NDK). However, an app cannot be created totally using C++ and the NDK is used to implement parts of the app in C++ native code.

Is OOPs important for Android development?

OOP Definition A few aspects of Object Oriented development,it can help to make the best of your Android projects, Objects and Classes. Inheritance.

Can Android develop with Java?

Use Android Studio and Java to write Android apps You write Android apps in the Java programming language using an IDE called Android Studio. Based on JetBrains' IntelliJ IDEA software, Android Studio is an IDE designed specifically for Android development.


1 Answers

Shouldn't classes all be contained in their own files?

Not necessarily as an Android Activity is a 'special case' class. If you haven't done already, I'd recommend you read Application Fundamentals and in particular the section on 'Activities' under Application components...

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

Note the section of text that I've highlighted in bold. The point is that an Activity in itself is not the complete app and if allowed, any third-party app can potentially invoke an Activity in one of your apps. As such, it is common to make an Activity as self-contained as possible. One particular example is the use of something like an AsyncTask which provides methods to execute a background thread as well as manipulate the UI - nesting a private class which extends AsyncTask is quite common and simplifies code. Nesting classes which extend BroadcastReceiver is also common for the same reason.

That said, there is nothing wrong with using separate Java class files for POJO helper classes, for example, it just comes down to how complex your app is but it can mean taking special consideration of how certain Android classes work - the AsyncTask class being one in particular if defined in a separate class file, try it and you'll see what I mean. :-)

like image 60
Squonk Avatar answered Oct 21 '22 16:10

Squonk