There are 7 life-cycle methods of activity.
They are as follows: - onCreate(),onStart(),
onResume() onPause() onStop() onRestart() onDestroy().
Intent i = new Intent(getApplicationContext(), ActivityTwo.class); startActivity(i);
onCreate()
This is the first callback and called when the activity is first created.
onStart()
This callback is called when the activity becomes visible to the user
onResume()
This is called when the user starts interacting with the application.
onPause()
The paused activity does not receive user input and cannot execute any code and called when the
current activity is being paused and the previous activity is being resumed.
onStop()
This callback is called when the activity is no longer visible.
onDestroy()
This callback is called before the activity is destroyed by the system.
onRestart()
This callback is called when the activity restarts after stopping it.
A Fragment is a piece of an activity which enable more modular activity design.
fragment is a kind of sub-activity.
A fragment has its own layout and its own behaviour with its own life cycle callbacks.
You can add or remove fragments in an activity while the activity is running.
You can combine multiple fragments in a single activity to build a multi-pane UI.
A fragment can be used in multiple activities.
onAttach()The fragment instance is associated with an activity instance.The fragment and the activity is not fully
initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization
work.
onCreate() The system calls this method when creating the fragment. You should initialize essential components of
the fragment that you want to retain when the fragment is paused or stopped, then resumed.
onCreateView() The system calls this callback when it's time for the fragment to draw its user interface for the first
time. To draw a UI for your fragment, you must return a View component from this method that is the root of your
fragment's layout. You can return null if the fragment does not provide a UI.
onActivityCreated()The onActivityCreated() is called after the onCreateView() method when the host activity is
created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point,
view can be accessed with the findViewById() method. example. In this method you can instantiate objects which
require a Context object
onStart()The onStart() method is called once the fragment gets visible.
onResume()Fragment becomes active.
onPause() the first indication that the user is leaving the fragment. commit any changes that should be persisted
beyond the current user session.
onStop()Fragment going to be stopped by calling onStop()
onDestroyView()Fragment view will destroy after call this method
onDestroy()onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be called by the
Android platform.
Basically fragments are divided as three stages as shown below.
Single frame fragments − Single frame fragments are using for hand hold devices like mobiles,
here we can show only one fragment as a view.
List fragments − fragments having special list view is called as list fragment
Fragments transaction − Using with fragment transaction. we can move one fragment to another
fragment.
using the putExtra() method from one activity and get the data from the second activity using the
getStringExtra() method.
What is onSavedInstanceState() and onRestoreInstanceState() in activity?
o onSavedInstanceState() - This method is used to store data before pausing the
activity.
o onRestoreInstanceState() - This method is used to recover the saved state of an
activity when the activity is recreated after destruction. So, the
onRestoreInstanceState() receive the bundle that contains the instance state
information.
When should you use a Fragment rather than an Activity?
o When you have some UI components to be used across various activities
o When multiple view can be displayed side by side just like viewPager
What is the difference between FragmentPagerAdapter vs
FragmentStatePagerAdapter?
o FragmentPagerAdapter: Each fragment visited by the user will be stored in the
memory but the view will be destroyed. When the page is revisited, then the
view will be created not the instance of the fragment.
o FragmentStatePagerAdapter: Here, the fragment instance will be destroyed
when it is not visible to the user, except the saved state of the fragment.
What are ViewGroups and how they are different from the Views?
o View: View objects are the basic building blocks of User Interface(UI) elements
in Android. View is a simple rectangle box which responds to the user’s actions.
Examples are EditText, Button, CheckBox etc. View refers to the
android.view.View class, which is the base class of all UI classes.
o ViewGroup: ViewGroup is the invisible container. It holds View and ViewGroup.
For example, LinearLayout is the ViewGroup that contains Button(View), and
other Layouts also. ViewGroup is the base class for Layouts.