Hacking Android Interviews (Activity)

Victor Apoyan
4 min readJul 27, 2020

Here I want to share with you interview questions which I was asked about Android activities, though they were asked several years ago, they are still actualโ€ฆ

Activities

โ“When the user press on the โ€œBackโ€ button will onDestroy() be called?

๐Ÿ™‹ Yes, when the user presses the โ€œBackโ€ button inside of activity it will call finish() which will close activity.

โ“ Which of the following methods is the best place to save data to file, in case if user close activity: onPause(), onStop(), or onDestroy()?

๐Ÿ™‹ The best place is onPause() function which is always called by the system when activity is closed. onStop() and onDestroy() can be not called depending on some situations.
**Remark: in the latest Android versions onStop() will be also called.

โ“How many states have Activity?

๐Ÿ™‹ The activity can have 4 states, they are listed below
1. active or running โ€” when activity is in the foreground of the screen.
2. paused โ€” when activity is still visible but lost the focus.
3. stopped โ€” when activity is no longer visible to the user and it can be killed by the system if additional memory needed.
4. resumed โ€” when activity is stopped or paused it can be killed by the system when it must be shown again, the system will restore it completely to the previous state.

โ“ How to bypass restarting of the activity depending on one or more configuration changes?

๐Ÿ™‹ By providing android:configChanges in the manifest file, in this case, you will receive onConfigurationChanged() event instead of onRestart().

โ“ How to start an activity and get a result from it when finished?

๐Ÿ™‹ By staring activity with method startActivityForResult(Intent, int) and handling result sent by method setResult(int) or setResult(int, Intent) when activity is finished in onActivityResult(int, int, Intent) method.

โ“What will happen if the activity was started with startActivityForResult(Intent, int) method but at some point crashed, will we receive onActivityResult(int, int, Intent) callback?

๐Ÿ™‹ Yes, the user will receive onActivityResult(int, int, Intent) callback with RESULT_CANCELD flag.

โ“ Imagine that you have two activities HomeActivity and DetailsActivity. First, you start HomeActivity, and then it starts DetailsActivity, describe the lifecycle of HomeActivity and DetailsActivity.

๐Ÿ™‹Below you can see the lifecycle of two activities:

โ“Imagine you have two activities ActivityA and ActivityB, ActivityA already starts ActivityB, describe the lifecycle of activities when user press back on ActivityB.

๐Ÿ™‹ Below you can see lifecycle of two activities:

โ“Imagine that ActivityA was destroyed at some point and some additional data were stored in onSaveInstanceState(Bundle) method, in which method(s) retained data can be restored?

๐Ÿ™‹ Retained data can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) methods.

โ“Are the methods below implemented correctly?

๐Ÿ™‹ The onCreate(Bundle) method will throw an exception as soon as the application starts, as savedInstanceState will be null, for the onRestoreInstanceState(Bundle) everything is okay because it is called only in the case if the application was restored.

โ“ In general when application is destroyed by system methods onPause(), onStop() and onDestroy() were called. Please describe such situation when methods onPause() and onStop() are not invoked.

๐Ÿ™‹ Such situation can happen if we call finish() from application onCreate() method, in this case onPause() and onStop() will never called.

โ“ Normally when the user rotates screen android system handles that situation and recreates screen after rotation, Imagine the situation that after rotation screen is not restored correct, what can cause such a situation?

๐Ÿ™‹The main reason for this issue can be the absents of android:id attribute for one of your Views.

โ“If you show an AllertDialog/ProgressDialog from your activity, will your activity be paused, will be onPause() method of activity called?

๐Ÿ™‹ No, Activity will not be paused in case if any dialog (Progress, Alert) is shown over it. Activity will go to pause if other activity even transparent will be shown.

Thatโ€™s it :) If you have more questions which you were asked while Android Interviews about activities, please post in the comments, so we can discuss them and help others to answer same question :)

--

--