Activity in android is a page that hosts content or layout that you design. Activity is a Java class, so it is considered as an object after it has been created. when you first create android app android studio will recommend you to create an activity (also a layout file automatically created that will be called inside this activity in java file) where the hellowold text is shown like in first app example make first android app.
Since activity is a java class, it has several methods as regular java classes. the most important method of activity is onCreate that will be called to create the activity. in this onCreate method you set the xml layout that you have created in layout folder (under res folder).
onCreate is the minimum method you should write to make activity show your layout content successfully. below is the source code example to make an activity class.
In the code above MainActivity is a kind of activity not because its name contains Activity but because it extends Activity class (in java Activity is called super class, and MainActivity inherits this super class).Since activity is a java class, it has several methods as regular java classes. the most important method of activity is onCreate that will be called to create the activity. in this onCreate method you set the xml layout that you have created in layout folder (under res folder).
onCreate is the minimum method you should write to make activity show your layout content successfully. below is the source code example to make an activity class.
package com.example.myapplication; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
when you call setContentView(R.layout.activity_main); , this activity begin to show your layout. in this example the layout activity_main.xml file under layout folder will show. if you don't call this setContentView then nothing will show at your activity(your activity just show the white blank color).
you should call super super.onCreate(savedInstanceState); because this method will derive all the basic functionality of activity to create activity.
Thank you for visiting our website. Just comment below if you have any question to ask.
No comments:
Post a Comment