Use Log.d in debugging
In “normal” Java, if we would like to see what happens in the program and where it “rolls over”, we would probably use the method of type
System.out.println (“Trololo, I’m after calculation”);
Android has no system console (javovej), but we have the ability to throw messages to LogCat in the same way. Just call this:
Log. d (“debugging a program”, “I am a message”);
In LogCata we will see the message:
What we give to the “d” method as the first parameter goes to the “Tag” column, what we will give as the second parameter goes to the “Text” column. This method works both when running a program on a virtual machine and on physical hardware attached to a computer.
Application life cycle
The activity can be in four states:
1. It is in the foreground
2. It is displayed but not in the foreground – for example when we display a small message box
3. Is off. It does not work, it is not in operating memory.
4. Paused. It is not closed, but it is not visible. Works in the background. This happens when we move from one activity to another. Activity left in this state can be killed at any time by the system, in case it needs memory that this activity uses.
The class inheriting from the Activity class will inherit several methods that are run when passing activity between states. We can override them. Enter the code shown below, run the application and then play with the device, ie turn it on, close the application, open it again, press “Home”, etc. See what happens when you do this on the LogCat console (located in the bottom the Eclipse screen).
The onCreate method is called when the activity is started for the first time. For this reason, it is most often used for this purpose, and it sets the appearance of the screen and the initial settings.
The onStop method is called when the application is closed. You can enter here the code that will write the application status, eg to the database.
The onPause method is invoked, for example, when we rotate the screen, and also before the onStop method when the application is closed.
The onResume method is also called when the screen is rotated , or when it returns to the action after the previous feed.
A complete diagram of the application transition across different states (from Android documentation):
“Toast” messages are used for short-term display of unimportant messages. The message appears for a few seconds and disappears, and the user may not even notice it, so you should not display such error messages. This is ideal for displaying, for example, hints.
It all boils down to the line:
Toast. makeText (getApplicationContext (), “I am a message …”, Toast. LENGTH_SHORT ) .show ();
Which is responsible for the whole message. The rest of the code shown in the image above is not relevant to us.
Here I did so that the message appears immediately after the program starts. Toast parameter. LENGTH_SHORT specifies the time of the message. Instead of Toast. LENGTH_SHORT can also give Toast. LENGTH_LONG , then the message will display slightly longer. In the first version it is about 2 seconds, in the second about 4. Unfortunately, it is not possible to manually set the display time of such message for a single application. As a result, the following is displayed at the bottom of the application:
Hints in editing windows
On some Android devices, we have a small screen. In such situations, there is no room for additional “balloons” with hints or descriptive fields. Alternatives may be hints in the editing windows themselves. The hint will be “grayed out” and will tell you what the window should be.
I’m starting with sticking one PlainText component on the screen:
Later on in the activity class, I use the “setHint” method of the EditText class. The content of line 14 serves only to refer to the object (I have to refer to it). The setting itself in place of the “onCreate” method will work from startup.
Handling a click event on a component
There are several ways to handle a component click event. I will present three most popular. For all three methods, we will need some component to which we will respond. For this reason, I stick one button on the screen.
The first simplest and probably the most transparent, but also the least readable and requires a lot of code. We create a separate click listener (ie, a click event listener – waiting for you to click and react) and attach it to the button:
What happens after a click is described in the onClickk method of the listener. In this case, it displays a Toast message with the text “om nom nom” (line 23). The hook of the created listener takes place on line 27.
Alternatively, though we may create a listener object at the moment of creation – line 19. Less code, but legibility is lost. The effect is the same as before.
The third alternative method is probably the least intuitive, but as you will understand the principle of operation is the code with more buttons to operate will be the most transparent and very comfortable to develop. This time I do not define a listener! Everything that happens after clicking the button is described in the Support method. It is important that this method accepts a View class object (after this class inherits all components such as a button and it is in the form of this parameter that is passed to the component object to the method) and returns nothing. Seemingly, the method as such is not in any way related to the click handler:
The magic begins only in the layout file associated with this activity:
We got here (line 19) new item: android: onClick. Instead of binding the Java method to the class from the specially created listener, we specify the method name using the button parameter in the layout file. This solution has the advantage that you write much less code, and the code itself with more buttons supported is readable. On the other hand, if we do not remember about this solution, we can go back in time as it was done.
Let’s keep in mind that click support is not just about buttons, but every component that inherits from View – including captions and edit fields.
Intentions – invoking screens and actions in the system
The intent is an intention to perform some action, such as sending an SMS, viewing a website, taking a picture, viewing another screen. Intentions can be of different types. Depending on your needs, we create the intent of the type (to take a picture, start the browser, call any other action) and then launch it. By the way, we can pass some parameters by intention. For example, from the list of all invoices, we launch a screen with a preview of one selected. We need to pass the invoice id to the display.
The first example will be a simple display of another application screen. I’m starting with a simple Android project. Then add a new activity by clicking PPM on the bundle and selecting New -> Other and from the window that appears, select Android Activity:
I assign the class name of this activity and the XML file where the Layout will be described.
Once approved for the project I get these two files:
A in the manifest file new entry:
Let’s go to code now:
Lines 24-31 is a call to action method call () at the push of a button. In the method itself, the reaction is written what happens in this situation. As you can see, I am creating a new Intent object (line 14) and in the next line I am triggering startActivity (inherited from the Activity class). Since the intent I am invoking is another activity, I have to pass it a context, and indicate the class of activity to be called. Do not forget, of course, that after creating the intention to call it 🙂 The second activity – the one I call is the simplest screen with the words “Hello World!”.
Let’s see how the application works. The application immediately loads my main activity:
Another activity was triggered.
Let’s get some of our application up. To call another screen I will now add a variable. I use the putExtra method for this. We must remember to call it before startActivity (Intent and). In this case, I pass the text: “I am a variable!” In the form of a variable named “variable”. This text will be passed to the currently called activity and displayed on the console (I will take care of this text at the level of this new activity.
To retrieve the transferred object, I create a Bundle object on the side of the called object that retrieves the transferred object set using the getExtras () method. From this bundle, I must now select the object that interests me (line 16). I received this text now from the LogCata console:
Effect after launching and clicking the button:
Let’s look at another example of activity. This time it will be a web page call. I give the system the intention to open a website:
The Android platform checks what the program is responsible for, and passes it on to. Since we want to make a connection to the internet, we need to make sure we add the appropriate permissions to the manifest file: