Download presentation
Presentation is loading. Please wait.
1
Activities, Fragments, and Intents
2
Activities Activity – window that contains the user interface of your application May have zero or more activities Many purpose of activity is to interact with user Activity Life Cycle
3
Fragments Android 4.0 introduced Fragments
Fragment is miniature activity that can be grouped to form an activity
4
Intent Intent – glue that enables different activities from different applications to work together seamlessly Used to call built-in applications such as: the Browser Phone Maps Etc.
5
Activity Activity base class defines a series of events that govern the life cycle of an activity onCreate() – called when the activity is first created onStart() – called when activity becomes visible to user onResume() – called when activity starts interacting with user onPause() – called when current activity is being paused and previous activity is being resumed onStop() – called when activity is no longer visible to user onDestroy() – called before activity is destroyed by system onRestart() – called when activity has been stopped and is restarting again
6
Activity import android.os.Bundle; Import android.app.Activity public class MainActivity extends Activity { /** Called when activity is first created. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
7
Activity By default, the activity created for you contains the onCreate() event. User Interface component is loaded using XML file defined in your res/layout folder – main.xml Every activity you have in your application must be declared in your AndroidManifest.xml file
8
Activity Life Cycle
9
Activity An Activity is destroyed when you click the Back button
The state the Activity is in will be lost Additional code is required to preserve its state when it is destroyed Guidelines to follow: Use onCreate() to create and instantiate objects you will be using in your application Use onResume() to start any services or code that needs to run while your Activity is in the foreground Use the onPause() to stop any services or code that does not need to run when your Activity is not in the foreground Use the onDestroy() to free up resources before your Activity is destroyed
10
Link Activities using Intents
In Android, you navigate between activities through an Intent An Activity is made up of: a UI component (main.xml) and a class component (.java file) AndroidManifest.xml file, add the Activity with an Intent <activity android:name=".SecondActivity" android:label="Second Activity" > <intent-filter> <action android:name="com.example.SecondActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
11
Link Activities using Intents
public void onClick(View view) { startActivity(new Intent("com.example.SecondActivity")); } Activities can be invoked by any application running on the device. Fundamental concept in Android startActivity() method invokes another Activity Does not return a result to the current Activity To pass data back from an Activity, use the startActivityForResult() method
12
Fragments
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.