Activities, Fragments, and Events
-
Upload
henry-osborne -
Category
Education
-
view
1.141 -
download
0
description
Transcript of Activities, Fragments, and Events
![Page 1: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/1.jpg)
Activities, Fragments, and Events
CPTR322: Mobile Application Development
Henry Osborne
![Page 2: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/2.jpg)
Topics Covered
• The life cycle of an activity
• Customizing the UI using fragments
• Applying styles and themes to activities
• Displaying activities as dialog windows
• Understanding the concept of intents
• Linking activities using intent objects
• Displaying alerts using notifications
![Page 3: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/3.jpg)
Understanding Activities
![Page 4: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/4.jpg)
What are activities?
• A window that contains the UI of an application• An application can have zero or more activities• Main purpose is to interact with the user
• Life Cycle: the stages an activitiy goes through from the moment it appears on the screen to the moment it’s hidden.
![Page 5: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/5.jpg)
Creating Activities
• To create an activity, a Java class that extends the Activity base class is created.
import android.app.Activity;…public class Activity101Activity extends Activity {
…
}
• The activity class loads it UI component using the XML file defined in the res/layout folder.
![Page 6: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/6.jpg)
Declaring Activities
• Each activity in the application, must be declared in the AndroidManifest.xml file.
<activityandroid:label=“@string/app_name”android:name=“.Activity101Activity”><intent-filter></intent-filter>
</activity>
![Page 7: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/7.jpg)
Activity Base Class
• onCreate() – Called when the activity is first created
• onStart() – Called when the activity becomes visible to the user
• onResume() – Called when the activity starts interacting with the user
• onPause() – Called when the current activity is being paused and the previous activity is being resumed
• onStop() – Called when the activity is no longer visible
• onDestroy() – Called before the activity is destroyed by the system
• onRestart() – Called when the activity has been stopped and is restarting
![Page 8: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/8.jpg)
Figure 1: Activity Life cycle
![Page 9: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/9.jpg)
Applying Styles and Themes
![Page 10: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/10.jpg)
Applying Dialog Theme
<applicationandroid:icon=“@drawable/ic_launcher”
android:theme=“@android:style/Theme.Dialog”><activity
android:label=“@string/app_name”android:name=“.Activity101Activity”><intent-filter></intent-filter>
</activity>
</application>
![Page 11: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/11.jpg)
Hiding Activity Title
import android.view.Window;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
![Page 12: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/12.jpg)
Linking Activities Using Intents
![Page 13: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/13.jpg)
Fragments
![Page 14: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/14.jpg)
What is a Fragment?
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity.getFragmentManager() and Fragment.getFragmentManager().
![Page 15: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/15.jpg)
Figure 2: An example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design.
![Page 16: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/16.jpg)
Adding Fragments Dynamically
• It is more useful if fragments are created and added to activites during runtime.
• Allows for a customizable UI
• E.g. If the application is running on a smartphone, you might fill the activity with a single fragment; if the activity is running on a tablet, you might then fill the activity with two or more fragments.
![Page 17: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/17.jpg)
Fragment Life Cycle
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments.
![Page 18: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/18.jpg)
Figure 3. The lifecycle of a fragment (while its activity is running).
![Page 19: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/19.jpg)
Displaying Notifications
![Page 20: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/20.jpg)
For important messages the NotificationManager is used to display a persistent message at the top of the device (commonly known as the status bar, sometimes referred to as the notification bar)
![Page 21: Activities, Fragments, and Events](https://reader036.fdocuments.in/reader036/viewer/2022081512/555ac727d8b42a761a8b4e9c/html5/thumbnails/21.jpg)
Activities, Fragments, and Events