Android Activity Frequently Asked Interview Questions Part 1 of. N

I Have taken most of the questions from people's interview reviews from glassdoor and combining them in one place.

1.What is the activity?/What do you know about the activity?

Ans: Activity is one of the building blocks of Android, Activity represents a single screen with a user interface, In a nutshell, whatever you see on the screen of your application is part of the activity(Button, EditText, TextView…other views are put in the activity).

2.What are the lifecycle methods of activity?

3.What are the differences between activity and fragment?

Ans: Fragment is a part of an activity that gives us the facility to divide your bigger screen into the smaller parts of different UIs is having their own lifecycle methods. An activity can contain multiple fragments.

For Activity System maintains the back stack.

For Fragment, the programmer has to maintain the back stack in the hosting activity.

4.What are the different launch modes available in android?

Ans: You can find a detailed explanation here.

5.Difference between onCreate() and onStart()?

Ans: onCreate(): It is the first method that gets called once you launch the activity, Here we do initialization of the components which you need only once for the lifecycle of activity and linking of XML to activity.

onStart(): This method gets called right after onCreate(), System calls this method once your activity is visible to the users as the app prepares for the activity to enter the foreground and become interactive.

6.Describe onResume() ,onPause(),onStop(),onDestroy()?

Ans: onResume(): When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.

onPause(): As soon as the activity loses focus this method gets called. In other words, This method gets called when your activity is in a partially visible state. Such as semi-transparent activity (such as a dialog) opens and dialogs from other apps take the focus away from your activity.

onStop(): When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running and is about to be terminated.

onReStart(): Gets called when Activity comes to foreground from the onStop() state here you can reinitialize components which you have shutdown in onStop().

onDestroy(): System calls this method when your activity is about to destroy and remove the activity from the task stack.

7.Explain onSaveInstanceState() and onRestoreInstnace()?

Ans: onSaveInstanceState(outState: Bundle?): Invokes when activity is getting destroy temporarily, The information you want to save when you change the orientation and want to restore later so you put information in key-value pair in this method.

onRestoreInstnace(savedInstanceState: Bundle?): It gets called on a new instance of the activity, Only when you have saved some data in onSaveInstanceState() in this method you can restore the data and populate into the UI.

7.What is the inheritance hierarchy of an activity?

Ans: YourActivity->AppCompatActivity->FragmentActivity->ComponentActivity->androidx ComponentActivity->Activity->ContextThemeWrapper->ContextWrapper->Context

8.Why do we need to define activity in the manifest?

Ans: There is a contract between your application and android system that you have to tell the system what all are the components you are going to use so those components you have to define in the manifest, Or in other words, you can say manifest is a bridge between your application and android system where you need to define metadata information of your application so that android os will permit you to use them.

9.What happens when we change the orientation of an activity?

Ans: If we change the orientation of the activity so activity gets temporarily destroy and recreates from the scratch.

10.Will that be a guarantee that onDestory() will always get a call?

Ans: No that is not a guarantee if the system needs more memory so it may terminate your application and in that case, onDestroy() will not be called.

11.What all lifecycle methods get called when you go from One Activity to Second Activity?

Ans: You can find a detailed explanation here.

12.What is App Compat activity?

Ans: AppCompatActivity. Base class for activities that wish to use some of the newer platform features on older Android devices. Some of these backported features include: Using the action bar, including action items, navigation modes, and more with the setSupportActionBar(Toolbar) API.

13.Why we don’t create objects of activity?

Ans: By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

14.What all methods are mandatory to override in an activity?

Ans: No mandate methods.

15.What is activity context?

Ans: Activity context is the context of the activity whose lifecycle is restricted to activity, You should use activity context if you want to perform some tasks in the scope of the activity.

16.In which thread activity runs?

Ans: Main thread

17.What happens to activity when you try to update data from a background thread?

Ans: Your application will crash and the exception message would be

Ony the original thread that created a view hierarchy can touch its views.

18.How to send data to the main thread from the background thread?

Ans: By using thead and handlers methods runOnUIThread() handler.post().coroutines etc.

19.What will happen if you call API in one activity and go to another activity without receiving the data in activity one?

Ans: Data loss.

20.What is the purpose of startActivityForResult?

Ans: To do some operation on the second activity and get the result back to the current activity.

21.How to access data of the activity in a fragment?

Ans:(activity!! as Activity).methodName()

22.What is use of onNewIntent() method?

Ans: It is will be delivering the new data to the activity.

23.What is different types of flag available for activity?

Ans:

24.Correlation between activity and fragment lifecycle.

Ans: http://landenlabs.com/android/info/activity-life-cycle/activity-life-cycle.html

25.If I call finish() from the following method call stack will be:

  1. onCreate()→onDestroy()
  2. onStart()→onStop() →onDestroy()
  3. onResume()→onPause() →onStop() →onDestroy()

--

--

--

Senior Android App Developer | Startup | Product base | Java | Kotlin | MVVM | Architecture components | Android Blogger

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a Joke App with Cloud Firestore using Kotlin part 2

Storage Roadmap: Targeting Android 30

Top 5 IDEs to Develop A Flutter App

Android Static Code Checks — Keep Your Codebase Tidy With Detekt

Getting Started with Android Development: Android MVVM Architecture

Android design and performance improvements

side-by-side android screens with a list of stages on the left and boxes in the “lead” stage on the right

How to Completely Uninstall Android Studio on Mac OS?

ConvertWebView To Image in Android — Step by Step Guide

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aalishan Ansari

Aalishan Ansari

Senior Android App Developer | Startup | Product base | Java | Kotlin | MVVM | Architecture components | Android Blogger

More from Medium

Run App using Wi-Fi (Android Studio Update)

Quest to a refined Outdoor Experience Android App (Strava and Relive inspired) — Part 3

How to perform search using channel for Paging 3

How to Get Started as an Android Developer