pratiktest
10/6/2016 - 11:20 AM

android.md

Activity

AppCompatActivity

  • Activity is always the baseline. Everything extends from activity.
  • Fragment activity is for the use of backport fragments.

FragmentActivity

Fragments

  • Fragments represent portion of ui in an Activity
  • multiple fragments can be combined in a single activity to create multi-Pane UI
  • advantage of fragment is that it can be reused in multiple activity
  • A Fragment has its own lifecycle, and receives its own inputs and output
  • Fragments lifecycle is affected by parent activity. If Activity is paused all its fragments are paused.If Activity is destroyed all fragments are destroyed
  • However we can manipulate the fragment once the Activity is in running state (remove add etc)
  • We can also add fragment to backstack. Backstack allows to reverse a fragment transaction, like back button
  • When we add fragment to activity layout, it lives in a ViewGroup ( A special view that contains other views)
  • there are 2 ways to insert a fragment in application layout
    • by declaring <fragment> element in your activity layout file
    • or by adding to View Group in your application code

Why Fragments

  • Fragments were primarily created for tablets since tablet screen is bigger, so there is more room for UI components to interchange
  • so with fragments we can preserve activity appearance in backstack. For eg one activity contains 2 fragments, 1 to select from list of news and other to view the article
  • each fragment should be seperate and modular. We should avoid manipulating one fragment from another fragment

Creating fragment

  • To provide a layout for a fragment, you must implement the onCreateView() callback method
public static class ExampleFragment extends Fragment {

   @Override
   
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                            
       // Inflate the layout for this fragment
       return inflater.inflate(R.layout.example_fragment, container, false);
       
   }
}
  • The container parameter passed to onCreateView() is the parent ViewGroup
  • The savedInstanceState parameter is a Bundle that provides data about the previous instance of the fragment, if the fragment is being resumed
  • The inflate() method takes three arguments
    • The resource ID of the layout you want to inflate.
    • The ViewGroup to be the parent of the inflated layout
    • A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation. Since we have already specified container in second argument, system will inflate the layout in container. Hence we specify this as false)

##Learning Android

  • code in java is compiled to Dalvic Executable (DEX). The part of android that executes compiled code is Dalvic Virtual Machine