Tuesday, April 1, 2014

ANDROID ACTIVITY

It is responsible for constructing the block UI. You can see  activity as the analog of a window in a desktop application. Activities have the responsibility of presenting the visual elements and react to user actions. While we can think of activities that do not have a user interface, the code regularly in these cases is packaged in the form of content providers  and services.


Source


Almost all activities interact with the user.  if you want to  create a window  you can use setContentView(View). the activity's views almost are presented to the users as full-screen windows, but you have another options like floating windows (windowIsFloating) or embedded inside another activity (ActivityGroup).

There are two methods almost all subclasses of Activity will implement:

  • onCreate is where you initialize your activity, setContentView(int) and findViewById(int).
  • onPause is here you deal with the user leaving your activity. The changes will be committed usually to  the ContentProvider.

The "callbacks" others are: 
  • onRestart  is where the user returns to navigate to the Activity, after being hidden by another Activity (onStop ()): execution is done just before onStart (). 
  • onStart  is where just before the Activity is visible (foreground) user. 
  • onResume  is where just before the Activity begins to interact with the user. 
  • onStop  is where when the Activity is no longer visible to the user. 
  • onDestroy  is where just before destroying the Activity
To be of use with Context.startActivity(), all activity classes must have a corresponding <activity> declaration in their package's. All activity is initiated in response to an Intent.


Activity Lifecycle


Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

  • An activity has essentially four states:
  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it ispaused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.


There are three key loops you may be interested in monitoring within your activity:

  • The entire lifetime of an activity happens between the first call toonCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy(). 
  • The visible lifetime of an activity happens between a call to onStart()until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user. 
  • The foreground lifetime of an activity happens between a call toonResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight. 
If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.
 public class MyActivity extends Activity {
     ...

     static final int PICK_CONTACT_REQUEST = 0;

     protected boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             // When the user center presses, let them pick a contact.
             startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 PICK_CONTACT_REQUEST);
            return true;
         }
         return false;
     }

     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // A contact was picked.  Here we will just display it
                 // to the user.
                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }
Excerpt from store a calendar activity. The user's preferred view mode in its persistent settings:
 public class CalendarActivity extends Activity {
     ...

     static final int DAY_VIEW_MODE = 0;
     static final int WEEK_VIEW_MODE = 1;

     private SharedPreferences mPrefs;
     private int mCurViewMode;

     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         SharedPreferences mPrefs = getSharedPreferences();
         mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
     }

     protected void onPause() {
         super.onPause();
 
         SharedPreferences.Editor ed = mPrefs.edit();
         ed.putInt("view_mode", mCurViewMode);
         ed.commit();
     }
 }
Excerpt permission:
AndroidManifest.xml: 
<uses-permission android:name="android.permission.READ_CONTACTS"/> 
Activity's code:
Cursor personas = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
null, null, null, null); 
while(personas.moveToNext()){ 
int indiceNombre = personas.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
String nombre = personas.getString(indiceNombre); 
Log.d("CONTACTOS", nombre); 

Build an Intent


An Intent is an object that provides runtime binding between separate components (such as two activities). The Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but most often they’re used to start another activity.

<application ... >
    ...
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>



No comments:

Post a Comment