Hello again .. i hope you all are fine 🙂
Today i will talk about something new .. in the last lesson we created an Activity that has a Button and a TextView .. but we didn’t define what’s activity ?!
Activity is a window that contains the user interface of your applications. An application can have zero or more activities, and the main aim of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle.
And there’s another definition that i will talk about .. it’s called intent ..
An intent is basically the “glue” that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application.
Now let’s talk about how to create an Activity .. to create an Activity we will create a java class that extends an Activity base class.
Our Activity class then load its UI components(Buttons, TextView, EditText, … etc) from the xml file created in res/layout folder(which is here named activity_main.xml) .. and every Activity in our application must be declared in AndroidMainfest.xml file
The Activity base class defines a series of events that governs the life cycle of an activity. The Activity
class defines the following events:
➤ 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
➤ onStop() — Called when the activity is no longer visible to the user
➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by
the system to conserve memory)
➤ onRestart() — Called when the activity has been stopped and is restarting again
We will see an example for better understanding the Activity Life Cycle .. 🙂
First we will create a new android project .. by default we will found onCreate() method .. we will add to it a line that print a message on LogCat to know which method will be executed first …
Log.d(tag, “In onCreate() Event”);
and we will add this code that call the rest of events ..
And then we will start debugging … when we see the application .. we will see 3 messages in LogCat
When we press the Back button in android emulator … another 3 messages will be displayed in LogCat
And when we press Home button in android emulator .. another 3 messages will be displayed in LogCat
But when we press the Phone button on the Android Emulator so that the activity is pushed to the background .. we will see that LogCat contain 2 messages
Now press the Back button again to exit phone dialer .. the LogCat will display these messages
- The onPause() event is called in both scenarios — when an activity is sent to the background, as well as when it is killed when the user presses the Back button.
- When an activity is started, the onStart() and onResume() events are always called, regardless of whether the activity is restored from the background or newly created.
Now we will talk about how to apply Styles and Themes to our Activity … Let’s see 🙂
To apply a dialog theme to an Activity .. we will just add one line in AndroidMainfest.xml inside our Activity
And this will produce the following output …
mmm but how to hide this title bar ?! .. The solution is to go to out onCreate method and under the line of super.onCreate(); .. we will put this line requestWindowFeature(Window.FEATURE_NO_TITLE); .. and this is the result … tadaaaa 😀
That’s enough for today .. i hope it’s useful .. enjoy learning 🙂