Android Actions interview questions and solutions | by way of Mohit Kansal | Jan, 2024

Mohit Kansal

1. What occurs to an Task when the House button is pressed?

Resolution: When the House button is pressed, the present Task is moved to the background, and the onPause() approach is known as, adopted by way of onStop(). The Task isn’t destroyed, however it may be killed by way of the device when reminiscence is wanted in other places.

2. What occurs to an Task when the Again button is pressed?

Resolution: When the Again button is pressed, the present Task is destroyed, and the former Task within the again stack is resumed. The onPause(), onStop(), and onDestroy() lifecycle strategies are known as in series.

3. How are you able to intercept the Again button press in an Task?

Resolution: You’ll intercept the Again button press by way of overriding the onBackPressed() approach for your Task. This permits you to carry out particular movements prior to the Task is done.

4. What’s the goal of the onUserLeaveHint() approach?

Resolution: The onUserLeaveHint() approach is known as when the person has clicked the House button or has decided on every other app during the Fresh Apps listing. It means that you can come across that the person is leaving your app.

5. How are you able to release an Task when the person presses the House button?

Resolution: Launching an Task when the House button is pressed is usually discouraged as it disrupts the usual navigation waft. On the other hand, it may be carried out by way of making a launcher Task this is began when the HOME intent is fired.

6. How are you able to save you the Again button from destroying your Task?

Resolution: You’ll save you the Again button from destroying your Task by way of overriding the onBackPressed() approach and no longer calling tremendous.onBackPressed() inside of that approach.

7. What occurs to an Task when the tool display screen is grew to become off?

Resolution: When the tool display screen is grew to become off, the onPause() approach is known as, adopted by way of onStop(). The Task isn’t destroyed, however it may be killed by way of the device when reminiscence is wanted in other places.

8. What occurs to an Task when a telephone name is gained?

Resolution: When a telephone name is gained, the onPause() approach is known as, adopted by way of onStop(). The Task isn’t destroyed, however it may be killed by way of the device when reminiscence is wanted in other places.

9. What occurs to an Task when a configuration trade happens (like rotation)?

Resolution: When a configuration trade happens, the Task is destroyed and recreated. The onPause(), onStop(), and onDestroy() strategies are known as, adopted by way of onCreate(), onStart(), and onResume().

10. How are you able to retain an Task’s state all the way through a configuration trade?

Resolution: You’ll retain an Task’s state all the way through a configuration trade by way of saving the state within the onSaveInstanceState() approach and restoring it in onCreate() or onRestoreInstanceState().

11. What occurs to an Task when every other Task is introduced on most sensible?

Resolution: When every other Task is introduced on most sensible, the present Task is paused and stopped, however no longer destroyed. The onPause() and onStop() strategies are known as in that order.

12. How are you able to make sure that an Task isn’t destroyed when the person rotates the display screen?

Resolution: You’ll make sure that an Task isn’t destroyed all the way through a display screen rotation by way of dealing with the configuration trade your self. This will also be carried out by way of including android:configChanges=”orientation|screenSize” within the Task’s declaration within the AndroidManifest.xml.

13. What’s the onUserLeaveHint() approach in Android?

Resolution: The onUserLeaveHint() approach is known as when the person navigates clear of the Task by way of urgent the House button or after a job transfer. This technique will also be overridden to accomplish movements that are meant to happen simplest when the person leaves the app of their very own volition.

14. What’s the distinction between onPause() and onStop()?

Resolution: onPause() is known as when the Task remains to be in part visual, however the person is most probably on their means out. onStop(), however, is known as when the Task is now not visual.

15. How are you able to save the state of an Task prior to it will get destroyed?

Resolution: You’ll save the state of an Task prior to it will get destroyed by way of enforcing the onSaveInstanceState() approach and saving the state knowledge as a Package.

16. What’s the onRestoreInstanceState() approach in Android?

Resolution: The onRestoreInstanceState() approach is known as after the onStart() approach. It restores the stored state of an Task from the Package that was once stored within the onSaveInstanceState() approach.

17. What occurs to an Task when the tool is going to sleep or when the lock display screen seems?

Resolution: When the tool is going to sleep or when the lock display screen seems, the onPause() approach is known as, adopted by way of onStop(). The Task isn’t destroyed, however it may be killed by way of the device when reminiscence is wanted in other places.

18. What occurs to an Task when a conversation seems?

Resolution: When a conversation seems, the Task’s onPause() approach is known as for the reason that Task loses focal point. On the other hand, onStop() isn’t known as for the reason that Task remains to be visual at the back of the conversation.

19. What occurs to an Task while you navigate to another app the use of the Fresh Apps listing?

Resolution: Whilst you navigate to another app the use of the Fresh Apps listing, the onPause() approach is known as, adopted by way of onStop(). The Task isn’t destroyed, however it may be killed by way of the device when reminiscence is wanted in other places.

20. What’s the onActivityResult() approach in Android?

Resolution: The onActivityResult() approach is known as when an Task you introduced exits, supplying you with the requestCode you began it with, the resultCode it returned, and any further knowledge from it.

21. What’s the onBackPressed() approach in Android?

Resolution: The onBackPressed() approach is known as when the process has detected the person’s press of the again key. The default implementation merely finishes the present process, however you’ll override this to do no matter you wish to have.

22. What’s the onUserLeaveHint() approach in Android?

Resolution: The onUserLeaveHint() approach is known as when the person navigates clear of the process without delay, no longer as a result of every other process is introduced in entrance of it. This technique is usually used to devote unsaved adjustments to power knowledge.

23. What’s the onPause() approach in Android?

Resolution: The onPause() approach is known as as a part of the process lifecycle when an process goes into the background, however has no longer (but) been killed. The counterpart to onResume().

24. When is onStop() approach known as in Android?

Resolution: onStop() is known as when the process is now not visual to the person. This will occur as a result of it’s being destroyed, or as a result of every other process (both an present one or a brand new one) has been resumed and is protecting it.

25. What’s the onDestroy() approach in Android?

Resolution: onDestroy() is known as prior to the process is destroyed. That is the general name that the process will obtain. It may well be known as both for the reason that process is completing (because of the person utterly pushing aside the process or because of end() being known as at the process), or for the reason that device is briefly destroying the process because of a configuration trade (comparable to tool rotation or multi-window mode).

26. What’s the onRestart() approach in Android?

Resolution: onRestart() is known as after your process has been stopped, previous to it being began once more. That is adopted by way of onStart() and onResume().

27. What’s the onResume() approach in Android?

Resolution: onResume() is known as when the process will delivery interacting with the person. At this level, the process is on the most sensible of the process stack, with person enter going to it.

28. What’s the onStart() approach in Android?

Resolution: onStart() is known as when the process is changing into visual to the person. It’s adopted by way of onResume() if the process involves the foreground, or onStop() if it turns into hidden.

29. What’s the onCreate() approach in Android?

Resolution: onCreate() is the place you initialize your process. Most significantly, right here you’re going to most often name setContentView(int) with a format useful resource defining your UI, and the use of findViewById(int) to retrieve the widgets in that UI that you want to have interaction with programmatically.

30. What’s the onSaveInstanceState() approach in Android?

Resolution: onSaveInstanceState() is known as to invite the process to avoid wasting its present dynamic state, so it might probably later be reconstructed in a brand new occasion of its procedure is restarted. If a brand new occasion of the process is later created, the knowledge you set within the Package right here might be to be had within the Package given to onCreate(Package) and onRestoreInstanceState(Package).

31. What’s the onRestoreInstanceState() approach in Android?

Resolution: onRestoreInstanceState() is known as after onStart() when the process is being re-initialized from a in the past stored state, as given right here in savedInstanceState. Maximum implementations will merely use onCreate(Package) to revive their state, however it’s from time to time handy to do it right here in spite of everything of the initialisation has been carried out or to permit subclasses to come to a decision whether or not to make use of your default implementation.

32. What’s the onPostCreate() approach in Android?

Resolution: onPostCreate() is known as when process start-up is whole (after onStart() and onRestoreInstanceState(Package) were known as). Packages will usually no longer put into effect this technique; it’s meant for device categories to do ultimate initialisation after the applying code has run.

33. What’s the onPostResume() approach in Android?

Resolution: onPostResume() is known as when the process resume is whole (after onResume() has been known as). Packages will usually no longer put into effect this technique; it’s meant for device categories to do ultimate initialisation after the applying code has run.

34. What’s the onKeyDown() approach in Android?

Resolution: onKeyDown() is known as when a key-down tournament has happened. In the event you go back true, the development is treated and might not be handed additional down the development chain.

35. What’s the onKeyLongPress() approach in Android?

Resolution:onKeyLongPress() is known as when a key lengthy press tournament is up. In the event you go back true, the development is treated and might not be handed additional down the development chain.

36. What’s an Task in Android?

Resolution: An Task in Android is a unmarried display screen with a person interface. It’s like a window in your utility. Every process can delivery every other process to accomplish other movements.

37. What’s the lifecycle of an Task?

Resolution: An Task has a number of lifecycle strategies which are known as when the process’s state adjustments. Those come with onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), and onRestart().

38. What’s the distinction between onPause() and onStop()?

Resolution: onPause() is known as when the process is now not within the foreground however remains to be visual, whilst onStop() is known as when the process is now not visual.

39. What’s an Intent?

Resolution: An Intent is a messaging object that you’ll use to request an motion from every other app part. You’ll use intents for all kinds of duties, however maximum steadily they’re used to start out every other process.

40. What’s the distinction between startActivity() and startActivityForResult()?

Resolution: startActivity() is used to start out a brand new process with out anticipating any consequence again. Then again, startActivityForResult() is used when you are expecting a consequence again from the brand new process.

41. What’s a Process in Android?

Resolution: A Process is a choice of actions that customers have interaction with when appearing a definite activity. The actions are organized in a stack — the again stack) — within the order through which each and every process is opened.

42. What’s the onSaveInstanceState() approach?

Resolution: onSaveInstanceState() is a technique used to retailer knowledge prior to pausing the process. This information is then handed to onCreate() if the process must be recreated, permitting the app to revive its state.

43. What’s a Fragment?

Resolution: A Fragment represents a behaviour or a portion of the person interface in an Task. You’ll mix a couple of fragments in one process to construct a multi-pane UI and reuse a fraction in a couple of actions.

44. What’s the distinction between a Fragment and an Task?

Resolution: Each Task and Fragment are parts of Android. On the other hand, an Task is a unmarried, centered factor that the person can do, whilst a Fragment is a modular a part of an process, which has its lifecycle, receives its enter occasions, and will also be added or got rid of whilst the process is working.

45. What’s Context in Android?

Resolution: Context is an summary elegance whose implementation is supplied by way of the Android device. It lets in get entry to to application-specific sources and categories, in addition to requires application-level operations comparable to launching actions, broadcasting and receiving intents, and so on.

46. What’s onActivityResult() in Android?

Resolution: onActivityResult() is a technique you utilize to obtain the outcome returned by way of an process that was once began by way of startActivityForResult(). It’s handed the request code you provided to startActivityForResult(), a consequence code it gained from the kid process, and an Intent, which is able to go back consequence knowledge to the caller.

47. What’s the distinction between end() and onDestroy()?

Resolution: end() is a technique you name to near the present process. onDestroy(), however, is a technique that is known as by way of the device to inform you that your process is being got rid of from the device reminiscence.

48. What’s onBackPressed()?

Resolution: onBackPressed() is a technique known as when the process has detected the person’s press of the again key. The default implementation merely finishes the present process, however you’ll override this to do no matter you wish to have.

49. What’s onConfigurationChanged()?

Resolution: onConfigurationChanged() is a technique that will get known as by way of the Android device when the tool configuration adjustments whilst your process is working. This may happen, for instance, when the tool display screen orientation adjustments from panorama to portrait.

50. What’s the function of the AndroidManifest.xml document?

Resolution: AndroidManifest.xml is a central document in an Android challenge. It gifts crucial details about the applying to the Android device, knowledge the device will have to have prior to it might probably run any of the applying’s code.

51. What’s an Task stack?

Resolution: An Task stack (Again stack) is a stack of actions which are recently working and stacked up in combination. The process on the most sensible of the stack is the process working at the display screen.

52. What’s startActivityFromFragment()?

Resolution: startActivityFromFragment() is a technique you name to start out an process from a fraction. The fragment handles the onActivityResult() callback.

53. What’s singleTask release mode?

Resolution: singleTask is a release mode the place the device creates a brand new process and instantiates the process on the root of the brand new process. On the other hand, if an occasion of the process already exists in a separate process, the device routes the intent to the prevailing occasion via a decision to its onNewIntent() approach, quite than growing a brand new occasion.

54. What’s single-instance release mode?

Resolution: singleInstance is a release mode the place a brand new process will at all times be created and a brand new occasion might be driven to the duty as the foundation one. On the other hand, if an occasion of process already exists in a separate process, Android will put the prevailing process within the foreground.

55. What’s onNewIntent() in Android?

Resolution: onNewIntent() is a technique that will get known as when the process receives a brand new intent whilst it’s already on the most sensible of the process stack. This occurs when an process has set its launchMode to singleTop.

56. What’s onUserLeaveHint() in Android?

Resolution: onUserLeaveHint() is a technique that will get known as when the person navigates clear of the process without delay, no longer as a result of every other process is introduced in entrance of it. This technique is usually used to devote unsaved adjustments to power knowledge.

Leave a Comment

Your email address will not be published. Required fields are marked *