Creating Android Activity

We have already develop an android project. In that project, we have created an activity called MainActivity. Now we have to open the MainActivity.java file as follows:

public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

In our application, the entry point is onCreate() method. When any activity is created then the android system provides the default implementation code to the onCreate() method. Here we can define our own custom code to perform the specific actions. We can write the code below the setContent(R.layout.activity_main); Always remember we have to call the following method inside the onCreate() method.

Super.onCreate(savedInstanceState);

This line is very important to run the application because this line is responsible to direct the base Activity class to build the work for the MainActivity class. If we don’t use this line then we get runtime error.

How Android displays the user interface

In android, for every action we have to provide the valid code. For instance, the activity has no idea how it is displayed on the screen; to display the activity on the screen, we have to set the content view for the activity which we want to display on the screen. We have to use the following line to show the activity on the screen:

setContentView(R.layout.activity_main);

In this, R.layout.activity_main is the layout file. It is an xml file that is located in the res/layouts directory.