Activity and Fragment Activity Life Cycle States of
- Slides: 13
Activity and Fragment
Activity Life. Cycle
States of an Activity 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 it is paused. 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 completely destroyed.
Specify Launcher Activity <activity android: name=". Main. Activity" android: label="@string/app_name"> <intent-filter> <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> For activity to be default specify : android: name ="android. intent. category. DEFAULT"
On Create • The system creates every new instance of Activity by calling its on. Create() method. • You should instantiate your class variables, declare the UI for the activity and congure the UI elements here. Text. View m. Text. View; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); // Set the user interface layout for this Activity set. Content. View(R. layout. main_activity); // Initialize member Text. View m. Text. View = (Text. View) find. View. By. Id(R. id. text_message); }
On Pause • The Activity is paused when the foreground activity is sometimes obstructed by other visual components. • This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. @Override public void on. Pause() { super. on. Pause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (m. Camera != null) { m. Camera. release() m. Camera = null; } }
On Resume • Called when focus comes back to the Activity • Should be used for acquiring back resources which werereleased on on. Pause() method and for setting upanimations, etc. which make sense when the user is interacting with the Activity @Override public void on. Resume() { super. on. Resume(); if (m. Camera == null) { // Local method to handle camera initialize. Camera(); } }
On Stop • The user opens the Recent Apps window and switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts. • The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the rst activity is restarted. • The user receives a phone call while using your app on his or her phone.
Start/Restart Your Activity • When your activity comes back to the foreground from the stopped state, it receives a call to on. Restart() • The system also calls the on. Start() method, whichhappens every time your activity becomes visible • Tip: Very uncommon for apps to use on. Restart()
Fragment • A Fragment represents a behavior or a portion of user interface in an Activity • You can think of a fragment as a modular section of an activity, which has its own lifecycle • Examples of Fragment are : Dialog. Fragment, List. Fragment, Preference. Fragment
3 Fragment Life. Cycle
Basic Fragment Code public static class Example. Fragment extends Fragment { @Override public View on. Create. View(Layout. Inflater inflater, View. Group container, Bundle saved. Instance. State) { // Inflate the layout for this fragment return inflater. inflate(R. layout. example_fragment, container, false); } }
Questions ?
- Podaj oznaczenie literowe nukleozydu
- Start fragment from activity
- Lifecycle fragment android
- Cross life cycle activity of system development is
- There were 11
- Southern states of america
- Big states vs small states guard against tyranny
- Which of the following is a sentence fragment marina
- What are fragments
- Subordinate clause fragments
- What is added detail fragment
- Compute tangent space per fragment
- Infinitive phrase fragment
- Sentence for guess