Answered step-by-step
Write java codes to show how the following classes are inherited...
1. Write java codes to show how the following classes are inherited from their main class in
Mobile programming. (10
marks) i. services
1. activity
2. broadcast receiver
3. content providers
4. fragments
Computer ScienceEngineering & TechnologyJava ProgrammingBBIT 311
Share Question
Answer & Explanation
Solved by verified expert
The Java code snippets, provided are inherited from the main classes in Android Mobile
Programming. However, it is important to note that these codes only show the basic structure of
a subclass inheriting from its parent class, and you would need to add your own code to define
the functionality of each subclass.
Note:
Depending on the version of Android you are targeting, you may need to use the androidx library
for fragments and other classes. In that case, you would need to import the appropriate androidx
classes and use those instead of the base classes shown above.
Step-by-step explanation
1. Inheriting from the Activity class:
public class MyActivity extends Activity {
// Your code here
}
2. Inheriting from the BroadcastReceiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
// Your code here
}
3. Inheriting from the ContentProvider class:
public class MyContentProvider extends ContentProvider {
// Your code here
, }
4. Inheriting from the Fragment class:
public class MyFragment extends Fragment {
// Your code here
}
Example codes for each of the four classes in Android Mobile Programming
Inheriting from the Activity class:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Your code here
}
// Other lifecycle methods and helper methods here
}
The Activity class is the base class for all activities in Android. When you create a new activity,
you typically create a subclass that inherits from Activity, as shown above. The onCreate()
method is one of the most important methods in an activity, as it is called when the activity is
first created. In this method, you typically inflate your layout using setContentView() and then
do any additional setup that is needed for your activity. You can also define other lifecycle
methods and helper methods in your subclass, depending on your needs.
2. Inheriting from the BroadcastReceiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Your code here
}
}