- What is Activity and there Lifecycle ?
- It is a single screen that represents GUI(Graphical User Interface) with which users can interact in order to do something like dial the phone, view email, etc.
- Activity lifecycle refers to the various states an activity can be in, such as onCreate, onStart, onResume, onPause, onStop, onRestart and onDestroy.
2. Activity Life cycle between two Activities ?
- when you start FirstActivity
- OnCreate() -> OnStart() -> OnResume() of FirstActivity will be called
- when you start SecondActivity using startActivity(new Intent(FirstActivity.this, SecondActivity.class))
- OnPause() of FirstActivity will be called and then
- OnCreate() -> OnStart() -> OnResume() of SecondActivity will be Called then
- OnStop() of FirstActivity will be called
- when you press back button on SecondActivity
- OnPause() of SecondActivity will be called then
- OnRestart() -> OnStart() -> OnResume() of FirstActivity will be called then
- onStop() -> onDestroy() of SecondActivity will be called
3. Use Cases for the OnStart() and OnResume() Callbacks methods in an Activity?
OnStart():
- Should be used for tasks that need to happen when the activity becomes visible, but before the user can interact with it.
- Good for tasks like UI updates, resource allocation(like network connection), and registering broadcast receivers
- Should avoid heavy operations that might delay the activity’s responsiveness.
OnResume():
- You need to refresh data or update the UI that should happen every time the activity gains focus.
- You want to start animations or transitions that are noticeable to the user.
4. Explain about Android Launch mode ?
It refers to the behaviour of how a new instance of activity is created and managed when it is launched by Intent. These launch modes are specified in the AndroidManifest.xml file using the android:launchMode
attribute.
Standard (default):
- In this mode, a new instance of the activity is created every time it’s launched, regardless of whether an instance of the activity already exists in the task.
- Each instance is placed on top of the task stack.
SingleTop:
- In SingleTop mode, if the activity is already at the top of the task stack, a new instance is not created. Instead, the
onNewIntent()
method is called on the existing instance to handle the new intent. - If the activity is not at the top, a new instance is created, and it’s placed on top of the stack.
SingleTask:
- In SingleTask mode, a new instance is created only if there are no existing instances of the activity in the task stack.
- If an instance already exists, the existing instance is brought to the foreground and the
onNewIntent()
method is called.
SingleInstance:
- SingleInstance mode is similar to SingleTask, but it’s more restrictive. Only one instance of the activity is allowed in the entire system.
- If an instance exists, it is used for any new requests, and the
onNewIntent()
method is called.
5. What is ANR in Android ?
- ANR stands for “Application Not Responding,”
- It is a term used in Android to describe a situation where an Android application is not able to respond to user input or system events within a reasonable amount of time.
6. What is Room Database ?
7. How to improve app performance ?
8. How to find memory leaks in the Android app?
9. How to secure token in Android ?