What is MainActivity.java in Android Studio?

MainActivity.java

Android App is completely based on the MVC pattern, under which the Visual Logic or Part of the application is visible completely from Program Logic and Data Storage . Is kept separately, so that all three can be developed and maintained independently. Visual Logic related Files from View , Program involving Logic Files on Controller Files related and Data Storage The Model is Represent name.
Whenever an Activity File is created in any Android App, two files are always created:
1. Java file related to controlling the activity such as java
2. Related to representing the Visual Part of the activity .xml file such as xml
That is why a Controller file named MainActivity.java is created as well as a View File named activity_main.xml , which defines the Layout of the first screen of the activity .
By the time we create a new Android App, the Java file Create named MainActivity.java by default is written in the following codes:
package com.android.dev; 

import androidx.appcompat.app.AppCompatActivity; 
import android.os.Bundle;

 public class MainActivity extends AppCompatActivity { 
@Override 
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 } 
}
All files of any Java application follow a certain directory structure and the root directory of this directory structure is known as Package. The URI ( Uniform Resource Locator ) of the package makes it possible to uniquely identify our application differently from any other application.
Therefore, independent of our Android App is however Java Files Create, they all live Stored within a Package, which we know as the Package Name and in each Java File for First Line Package to Specify with Keyword . As done by the following package statement in Current Android App:
package com.android.dev;
Since all Java files related to our Android App can be managed properly, when creating a new Android App, the first screen specifies the name of the Android App as follows on the screen as well as automatically determines the Package Name related to our Android App. Which is common usage for all Java files of the entire Android App:
m1
Activity is basically a Base Class of Java that meets the requirements related to defining and controlling the Visual and Interactive part of our Android App. In simplest terms, the activity is like the form of a Visual Basic Application, through which the user interacts with our Android App.
All the functionalities related to creating an activity have been specified in Java’s Activity Class, which is available in the Activity Package, and any Activity can be created only when the Package related to this Activity Class is imported into the current Java File. Have gone That is why the AppCompatActivity class of the app package related to creating the activity is imported through the next statement in the MainActivity.java Class File :
import android.support.v7.app.AppCompatActivity;
The AppCompatActivity Class has been imported in place of the Activity Class by this Import Statement so that the Target Android Platform for which we are creating our Current Android App, if this App is preceded or followed by that Target Android Platform Android Version But even if installed, the Android system of the Android device with that unwanted Android platform can also run this Android App in Compatible Mode .
The Import Statement with this Compatible Mode is created because we had checked the Backwards Compatibility (AppCompat) checkbox as shown in the following figure while creating a new Android App through Android Studio :
So after creating Android App, the MainActivity.java file to be created in this way would have Auto Generated Code as follows:
package com.android.dev; 

import android.app.Activity; 
import android.os.Bundle; 

public class MainActivity extends Activity { 

@Override 
         protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
     } 
}
In which we can see that this time the Activity Class Import has been done in place of AppCompatActivity . Apart from this class, another class named Bundle has been imported from android.os Package through the following statement :
import android.os.Bundle;
In this class, those functionalities have been defined, through which data is passed from one activity to another or the data related to the current state of the app is saved. For example, when our Android App is running, at the same time because of a phone call, all the data related to the state of our current Android App is saved in the background by the Bundle Class itself, so that When the user came to our Android App again after the phone call was disconnected, he got it in the same condition as the phone call was before it.
In simplest terms, the entire Android Development System is just a framework where every Android App is a Java application based on that certain framework, just like Java Applets used to be.
That is, the way we used to derive a class named Applet to develop Java Applets and override certain methods ( init, start, stop, destroy, update, paint and repaint ), just like Android App. To develop we have to derive the Activity or AppCompatActivity class and override certain methods ( onCreate, onStart, onRestart, onResume, onPause, onStop and onDestroy ). Just as the Java Applet used to run according to a certain lifecycle on the Underlying JVM, similarly the Android App is also run according to its lifecycle on the Underlying Android JVM.
Therefore, when a new activity is created, after importing all the required packages, a MainActivity Class is created and is inherited from the Automatically Activity or AppCompatActivity Class through the following statement :
public class MainActivity extends Activity {
Since Activity Class holds various methods related to the entire lifecycle of an activity, all members of the Activity Class ( onCreate, onStart, onRestart, onResume, onPause, onStop and onDestroy ) are MainActivity, as the MainActivity Class derives from the Activity Class. Inherit in class.
As a result, we have to implement only these methods to control different conditions of MainActivity and since MainActivity is related to the First Screen of any Android App, that’s why the onCreate () method implements Automatically to create the First Screen. it happens:
@Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); }
The first written @Override in this code is an Annotation, which is only applied so that our Android Compiler, before compiling the current Android App, makes sure that the onCreate () method has the Signature ( Output Type, Method Name, Number of Parameters and Types of Parameters ) are Exactly the same or not as they have been specified in the Activity Class from which the MainActivity Class is derived .
Signature and Super Constructor Statement of this onCreate () method are specified in any activity of every Android App in an Exactly similar way. That is, in any activity the onCreate () method is Exactly as follows:
@Override 
        protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); // Other Code }
In the last line of this onCreate () method , the setContentView () method is called and this is the method that determines which Layout will be used as the View for the Current Activity, which is the User to the User. Will appear as an Interface Screen.
Whatever Android App we create, all the resources of that Android App are represented by a class named ” R ” by the Android Framework and all the resources related to our current Android App are named ” res ” as shown in the next figure. Live in Folder, which can be referred by a class named R :
That is , all the resources in the directory named ” res ” of our Android App, we have to use this “R” Class in order to refer them to any Java file of our Android App and because this class named R It represents only Static Resources, so all members of this class are public static .
As a result, in order to refer to any resource in our Java class file, we have to directly specify the name of the resource with the name of the class using Dot Operator. That is, we do not need to create an object of R Class because all public static members of this class are equally available for the entire Android App.
Since our Current Android App’s MainActivity.java File has to set the View File named Associated activity_main.xml as First Screen, which along with the MainActivity create a sub-directory named ” layout ” of the ” res ” directory The location is located in Java file and can be referred to via R.layout.activity_main statement .
MainActivity
That is why in the setContentView () method of the MainActivity.java File, the R.layout.activity_main statement is specified as Parameter, as a result, when running this Android App, the layout specified in the activity_main.xml file , Displays as First Screen.
Since each Layout File of our Android App is an XML File, it is not necessary to specify .xml Extension with that View File in this setContentView () method. Just as we referred the activity_main.xml file to the MainActivity.java File, similarly other resources related to our Android App are also in the ” res ” directory of the Hierarchy of Android App . So even if we have to refer to another resource file, we have to use the same method.
For example, if we Mipmap the Folder Ic_launcher lay Refer to File, the way R.layout by ” / Res / Layout / ” Directory had Represent, in the same way R.mipmap by ” / Res / Mipmap / the” Directory Can also represent and refer to the ic_launcher file by R.mipmap.ic_launcher Statement . Also, you can represent other directories of ” res ” in the same way .