Android interview common Q&A

I've been through several job interviews recently and I want to share some typical questions for Android developer.
I hope it helps someone.


What's an activity / fragment

Typical question that you get on every interview as junior.
Activity is one screen of android application, you can navigate from one activity to another. Activity can contains one or more fragment as part of layout. Fragments were introduced in Android 3.0 as they are extremely useful when layouting for tablets, eg. application can show 2 fragments on tablet in ladscape mode and only one fragment for mobile.
It's recommended by Google to have single activity and just swap fragments using fragment transactions, or preferable navigation components.

 

What is listener / typical use?

A method that listens to some event, eg. onClickListener, onTextChangeListener, ...

 

What is AndroidManifest

The manifest is mandatory for every application. It defines all informations that are needed to install and integrate app to the system. It contains definitions for each activity, at least one main intent to launch the app, permissions, broadcast receivers, deeplinks, min. OS version, ...

 

What is ProGuard?

By default Java bytecode can be easily decompiled and read in almost original format. ProGuard helps with this by obfuscating (or more likely minimizing) the code by changing variables and methods name. It also shrinks the app by removing unsued parts of code. ProGuard rules can be set to keep some classes or methods untouched.

 

Implicit vs explicit intent

Implicit intent doesnt directly specify component to handle it. Android system searches for all components which are registered for the specific action and the data type.If many components are found then the user can select which component to use.
Explicit intent explicitly specifies it's receiver. Is typically used to start another activity.

 

What is NetworkOnMainThreadException?

Long running and blocking operations shouldn't be run on the main thread. Network calls are explicitly forbidden since Android 3.0 and throws NetworkOnMainThreadEception.

 

Options for long running tasks and background processing on Android

Long running tasks should be done on worker thread to keep application responsive. There are few ways to handle this.
- handle threads manually (thread pool, executors)
- AsyncTask, handled by ThreadPoolExecutor - deprecated in API lvl 30
- Kotlin coroutines - preferred way in 2020
- IntentService - handle intent on worker thread by default, useful for downloading files

 

What' wrong with this code?

public class MyFragment extends Fragment {
	private String name;

	public MyFragment(String name) {
		this.name = name;
	}

	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_my, container, false);

		TextView textView = (TextView) view.findViewById(R.id.name);
		textView.setText(name);

		return view;
	}
}

On configuration changes (eg. screen rotation) the system tries to kill and recreate an activity by calling default empty constructor. If there is no such constructor runtime exception will occur. Even if we specify empty constructor, the parameter passed by constructor is lost after recreate.
Parameters should be passed via bundles and Fragment.setArguments method. Best practice is to have a static method newInstance() for each fragment.

 

What are shared precerences, how is it stored?

Simple key-value storage, stored as xml file.

 

ViewHolder pattern and what we need to take care of.

ViewHolder pattern is used in RecyclerView, where the rows are recycled and reused during scrolling of list.
When recycled view is used, it still has old data inside. If some view should be empty, we need to set it to empty state.

 

What shouldn't be done in View.OnDraw method?

OnDraw method needs to be as fast as possible, so we shouldn't make new object or allocate anything there.

 

Lifecycle methods order?

activity lifecycle

 

What is retained instance?

Fragment can be set to retain during configuration changes when uderlaying activity is destroyed and recreated by Fragment.setRetainInstance(boolean).

 

How to retain data during configuration changes?

- persistent storage (db, network)
- override onSaveInstanceState() and onRestoreInstanceState()
- keep data inside viewModel that survives configuration changes
- set fragment as retained

 

What useful libraries do you know?

Retrofit (Ktor), OkHttp, Room, Android Jetpack libraries, image loading (Picasso, Glide, Coil), serialization (Gson, Moshi, kotlinx.serialization), epoxy, ...

 

What is Service and what thread does it run on?

Service is something that keeps running after activity or even app is killed. By default service is running on the main thread. Special case is intent service, that is running on worker thread.

 

Non-Android questions
- oop, object vs class
- garbage collector
- Java strong/soft/weak/phantom reference

Comments