Download presentation
Presentation is loading. Please wait.
1
Activity and Fragment
2
Activity LifeCycle
3
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.
4
Specify Launcher Activity
<activity android:name=".MainActivity" <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"
5
On Create The system creates every new instance of Activity by calling its onCreate() method. You should instantiate your class variables, declare the UI for the activity and congure the UI elements here. TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the user interface layout for this Activity setContentView(R.layout.main_activity); // Initialize member TextView mTextView = (TextView) findViewById(R.id.text_message); }
6
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 onPause() { super.onPause(); // 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 (mCamera != null) { mCamera.release() mCamera = null; }
7
On Resume Called when focus comes back to the Activity
Should be used for acquiring back resources which werereleased on onPause() method and for setting upanimations, etc. which make sense when the user is interacting with the Activity @Override public void onResume() { super.onResume(); if (mCamera == null) { // Local method to handle camera init initializeCamera(); }
8
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.
9
Start/Restart Your Activity
When your activity comes back to the foreground from the stopped state, it receives a call to onRestart() The system also calls the onStart() method, whichhappens every time your activity becomes visible Tip: Very uncommon for apps to use onRestart()
10
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 : DialogFragment, ListFragment, PreferenceFragment
11
3 Fragment LifeCycle
12
Basic Fragment Code 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); }
13
Questions ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.