Android Activity Frequently Asked Interview Questions Part-1

Android Activity Frequently Asked Interview Questions Part-1

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?

No alt text provided for this image


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.

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 the activity and?linking of XML?to the 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 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 dialogue) opens and dialogues 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 the foreground from the?onStop()?state?here you can reinitialize components which you have shutdown in?onStop().

onDestroy():?The 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?destroyed 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 destroyed and recreates from 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.

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

Only 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 thread 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 the use of the onNewIntent() method?

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

23. What are the different types of flags available for activity?

Ans:

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()


Tushar Singh

DSA in C++ ?? | Aspiring Data Analyst ?? | Advanced SQL ?? | Python | C++ | Kotlin

4 个月

Thankyou so much sir! It's going to help me and others too. ??

回复
Chandra Sekhar Bala

Android app Developer ???? | ex @Elrick @GoGlobally, @Nashbud | Open Source | Kotlin ?? | Java | web3 ??

1 年

Q no 13 ??♂?

回复
Alok K.

Android Developer at Deloitte USI || Android || Kotlin || Java || Android app Deployment to Playstore || Helping hand for freshers

1 年

Ques23 is unanswered.

回复
Hafiz Awais Khan

Sr. Android Developer | Kotlin | Java | Jetpack | MVVM | MVP | MVC | Clean Architecture | Dagger 2 | Koin | Dagger-Hilt | Room | SQLite | REST API | Coroutine | CI/CD | Git | Chat | Augmented Reality

1 年

sorry to say, i deffer from your point of view , these days things are different no one will ask these questions.

回复
Hossam Qandeel

Java Developer at NexaTG | Backend | Kotlin | SQL | NoSQL | Spring Boot | Microservices | Kafka | Docker | Kubernetes | Telecom

1 年

要查看或添加评论,请登录

社区洞察

其他会员也浏览了