Android App Life Cycle

App Components



                                            Intents
                                                |
                                                v
App             <-  Activities  ->          Receivers
                                                |
                                                v
                                ->          Services
        ^               ^
        |               |
        v               v
        Content Providers




Priorities

    Active Activities,                    Foreground Service
    Visible Activities,                   Running Service
    Background Activities



Adroid App Life Cycle



    onCreate -> onStart <-..-> onStop -> onDestroy
 
         : Partly covered by another Active app :
        --  onPause  -->            --  onStop  ->
Active                      Visible                 Background
        <-- onResume ---            <-- onStart --



Fragment

                    Back Stack                    |        Activity
                                                  |    Visible     Active
onAttach -> onCreate  -> onCreateView  ->         |
                            onActivityCreated ->      onStart ->  onResume
                                                  |
onDetach <- onDestroy <- onDestroyView    <-          onStop  <-  onPause

 
Note: onCreateView: Create view resources, data connections



App Outline


  • An activity is a single, focused thing that the user can do and roughly maps to one screen of the app.
  • Fragments are modular sections of an activity, usually meant to display UI.
  • A view is the basic building block for user interface components.
  • Buttons, Text, List and other widgets are subclasses of views and can be combined in ViewGroups to create larger layouts:
    • Frame Layout: Useful for Simple layout - Single (child) view or Stack. Views are all aligned against the frame boundary.
    • Linear Layout: Stacking views vertically or horizontally one after another; breaks up the display proportionately.
    • Relative Layout: Complicated - Allows the positioning of views relative to other views (parent/sibling) or the boundaries of the view.
  • Adapters translate a data source into views for a ListView to display.


Android Studio => New => Project => Blank Activity with Fragment.

MainActivity {
    onCreate()                  { setContentView activity_main;  add(container, Fragment); }
    onCreateOptionsMenu()       { inflate(menu.main); }
    onOptionsItemSelected()     { switch itemID: corresponding startActivity(); }
    AndroidManifest.xml         activity & intent-filter
} // MainActivity

 
Fragment {
    onCreate()                  { setHasOptionsMenu(); }
    onCreateOptionsMenu()       { inflate(menu.fragment); }
    onCreateView()              {
                                    textView.setText(mTextStr);
                                    // Or, setup Adapter:
                                    mAdapter = new ArrayAdapter(context, item_layout, item_view, new ArrayList<String>());
                                    rootView = inflate(fragment_main);
                                    listView = rootView.findViewById(listview);
                                    listView.setAdapter(mAdapter);
                                    listView.setOnItemClickListener() {
                                        detailActivityIntent.putExtra(mAdapter.getItem(position));
                                        startActivity(detailActivityIntent);
                                    }
                                }
    onOptionsItemSelected()     { new AsyncTask().execute(); }
    onStart()                   { new AsyncTask().execute(); }
    class AsyncTask {
        doInBackground()        { return result;  // runs in worker thread to process long background task. }
        onPostExecute()         { mAdapter.add(result); // runs in UI thread to deliver the results to UI. }
    } // AsyncTask
} // Fragment


Sample Code / Reference used for study

Android Studio => Demo app Sunshine
https://github.com/udacity/Sunshine-Version-2/


 
 
 


No comments:

Post a Comment