Mobile development is an interesting field. Clients will often think that now that you have your iOS app built you can quickly build it on Android - obviously that is not the case. Sometimes, however, there is no way around it; you need an Android app or a skill set and the only way you are going to get it is to give it a go. Perhaps you are an indie who has had some success on iOS and now wants to give Android a try. Maybe you are a professional developer who works on iOS everyday and wants to see what it's like on the dark side. Or perhaps you are the lone mobile developer working at a larger firm, and despite everything you say, your boss just says "well work it out". You find yourself googling "Android for iOS developers", or "Equivalent of UIViewController on Android". Luckily, many of the patterns in mobile development exist across all platforms, and you will quickly find yourself getting up to speed.
This blog post is intended to help you make the transistion easier. Through a series of quick tips you should be well on your way to developing on Android.
First, a quick overview of Android to get you started.
Getting setup with an IDE
The IDE of choice these days is Android Studio. A while ago, it was difficult to get started on Android; you had to choose an IDE from the countless Java IDE's and then download multiple files to get your SDK setup. Thankfully, those days are gone! Android Studio will set up the entire environment for you, and is a fantastic IDE. Based off IntelliJ, from JetBrains, it brings powerful features that are not even available in Xcode, such as deep refactoring support and better code completion. It even has code analysis similar to the static analyser to tell you when you are doing things wrong. If you have used AppCode the experience will be very similar.
The language
The language of Android is Java. Technically it is not Java, but a language that is syntactically identical to Java created by Google. You will find because it is not actually Java, but a clone that some of the more recent features of Java, such as lambdas, are missing. These features are often available via third party frameworks.
The simulator
One thing you will quickly notice is that the Android simulator is horrible. It is awfully slow, especially on first launch and to be honest is not an option for everyday development unless you are mad. Do yourself a favour and get the far superior Genymotion. It works just like the iOS simulator, but it is Android. You can setup devices from a whole range of pre made options to simulate whatever set of specs you want.
UIViewController
The equivalent of UIViewController on Android is Activity
. This is where you will do all your binding from your model layer to your views, and handle app lifecycle events.
Transitioning to another Activity
There is no UINavigationController on Android. The OS maintains a stack of Activities that you can push onto and the user can use the hardware back button to go back. So far it sounds similar to iOS but there is one big difference - on Android you can actually open another app's activity to handle data for you. The user can use the hardware back button to go back before your app was even opened. The navigation stack exists beyond your app, it is handled by the OS.
To push an activity you use an Intent
. Intents are sent to the OS to let it know that you want to show another activity. They are created like follows in your activity
Intent intent = new Intent(this, OtherActivity.class);
intent.startActivity();
Passing data between Activities
You are probably now thinking "thats great but what if I want a reference to that activity?". How do you pass data between activities? The answer to that is with Extras.
You can add extra objects to your Intent.
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("index", dataObject.index);
intent.startActivity();
In your new activity you extract the extras in the onCreate
method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int index = extras.getInt("index");
// Do something with the index
}
}
Opening another app
As previously mentioned, you can even open activities from other apps. Here is an Intent
to open the dial screen.
String uri = "tel:0212345678";
Intent intent = new Intent(Intent.DIAL);
intent.setData(Uri.parse(uri));
startActivity(intent);
Logging
The Equivalent of NSLog
on Android is the Log class. You can log at a number of levels such as Debug, Warning, and Error.
Log.d(TAG, "Main activity started");
TAG
can be any tag you want. It is used to filter the logs as when you view the log on Android you get the logs from every app. A good filter goes a long way.
Supporting older Android versions
On iOS, when a new OS is released, it has new features and if you want those features you have to upgrade. On Android new features are backported through the Android Support Libraries. Using the support library version of an API, rather than its native SDK version when it is available is considered best practice as it allows for more compatibility with older devices.
Conclusion
Android development really is not as bad as many iOS developers think it is - infact it is even quite enjoyable! Give it a go and see what you think. You should find yourself creating apps in no time.