Do all Android classes extend Activity? - java

I'm going through some Android development tutorials, and I notice that some classes extend Activity, and others extend ListActivity for example.
By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?
Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?
I read a few similar questions but it wasn't quite clear.

By "default", all Android Activities extend Activity.
By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?
You have to explicitly state that it should extend Activity if it does not already and if you plan to use it as an Activity. Kindly note that you there are times you wouldn't do it. For example, when you have a class for your objects or whatnot, then you don't need to extend Activity any longer.
Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?
ListActivity, AppCompatActivity, and any other class that ends in -Activity are "children of the Activity class. So by extension, extending by these "children" would already extend from the parent Activity class and you'll no longer need to do something like YourActivity extends ListActivity, Activity. I think Android Studio will give you an error if you do.
The difference between an Activity and a ListAcivity is that a ListActivity will require you to override several methods because it is expecting a ListView in its content.

Android is written in Java, and like all java classes, every class inherits from the Object class.
Does a newly created class implicitly extend Activity or do you need to explicitly extend it?
You need to extend it.
Does ListActivity already extend Activity?
Yes.
You can see class hierarchy for ListActivity here.

Newly created classes do not extend anything. ListActivity is a subclass of Activity so all of the functionality Activity has, ListActivity inherits. In the documentation for ListActivity you can see that it is a subclass of Activity.

In Android, All the classes are not extended by Activity.
Only the screens which render the UI are extended by Activity. So, if you want to create a UI screen then you have to explicitly extend it with Activity class and override the callback methods.
ListActivity is subclass of Activity which is only used when you have only a ListView in the your layout.It has some specific listview related methods.
Similarly, if you wanna create a fragment, your class will extend the Fragment Class and override its methods. Similarly for Service, BroadcastReceiver etc.

Related

Where to put logic that is repeated amongst my Fragments?

I have a tab-based application with 5 tabs (Fragments). Each fragment is very similar in appearance and functionality. Where should I put this logic for these classes so that I am not repeating it in each Fragment?
Should I be putting it in a parent class that extends fragment and then have each Fragment inherit from that class? Is this good android app design or is there a better idea I haven't thought of?
Should I put the logic in my MainActivity since it controls all my Fragments?
Have a class which inherits from Fragment, put all the shared logic in that, and then derive your final classes from it.

How to extend class with two another classes?

I want to extend my class with 2 other classes Fragment and Activity.
I tried this:
public class Frist extends Fragment , Activity
{
.....
.....
}
But it's not working. How can I fix it?
You can't extend multiple classes. Why you can't extend fragment in another class?
You should use decorator or composition pattern
Java does not support multiple inheritance
Fragment and activity both are completely different thing.
If your want to make activity having fragment over it then in android studio create project with Activity with Fragment instead of with Blank activity
Then Automatically android studio will provide with some code having fragment and classes and xml for it.

android -what approach to be used for some java code that repeats in many activities

I have an app that has a top menu as shown in the fig below. This is almost constantly used in all activities. For layout I have defined it in a single xml file and use <include>to include it in every layout file.I want to know if there is a way in java coding to avoid declaring all the imageButtons, and then using findViewById and onclick events for them , in each activity. The top four icons will act as menu and be available in all layouts and activities
First of all, what you are trying to achieve is against the android standards and this could affect the user experience for Android users. Instead, add the menu items on the action bar.
Anyway, you can achieve what you are looking for by defining a base class (named it like ActivityBase) and do all the initializations and listeners registrations on it. Then, extend from that base class. Bear in mind that each activity will have its own instance of the views of the base class and the state will differ from activity to another.
Although you have accepted an answer I disagree that the Application class should be used to host global methods. It can be used for maintaining global state but that's a different matter.
As others have said, use inheritance by creating a base Activity class then extend that for all of your other Activities. To make things easier, however, you can define the onClick method to be used for each of your buttons in the layout file itself by using (for example)...
android:onClick="myOnClickMethod"
As long as the base Activity defines a public method called myOnClickMethod (or whatever method name you choose) with a void return and which accepts a View parameter it will work without the need to implement View.OnClickListener and without having to set the listener in Java code. Example
public class MyBaseActivity extends Activity {
public void myOnClickMethod(View v) {
// Get the resource id of v and use switch / case to perform action needed
}
}
That's all you need.
Write it only in the first activity. Extend it to the other activities instead of extending with android.app.Activity.
Eg:
public class SecondActivity extends MainActivity{
}
Put that method in MyApplication class which extends Application. So, that it can be accessible by multiple activities.

How to manage activity inheritance in Android Development

This is a relatively general question that I have regarding to Android development.
In the Android application, I am using SlidingMenu library. Imagine the activity I am trying to implement has a Navigation Drawer (from Sliding Menu Library) and action bar tabs with View Pager and contains different fragments.
In order to have the navigation drawer in the activity, I had to inherit the application from SlidingActivity like this:
public class ActivityMain extends SlidingActivity implements TabListener {
However, to make View Pager work in this activity, I will need to use Make a FragmentPagerAdapter instance, and if I want to use it, it requires the activity extend the Fragment Activity.
My Activity already extended SlidingActivity, so there is no way to extend another super class. I am not sure what will be a proper way to solve this conflict. When I was working on the Android app, I have saw some other cases that different components in one activity requires to extend from different super class. What will be the general solution to such problem?
Thank you
If you look at the package of SlidingMenu here, they have an activity called SlidingFragmentActivity. Extend this activity instead of SlidingActivity gives you everything you need.
You can wrap your Activities in a SlidingMenu by constructing it programmatically new SlidingMenu(Context context) and then calling SlidingMenu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT). SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.
from https://github.com/jfeinstein10/SlidingMenu

communicate between 2 classes in android

ok I have 2 classes
an activity class and another class which extended from maps.Overlay. so the second class cannot be extended from first class because it already extended.
my second class gets the geo coordinates from user touch location on a map. what I want to do is show a window(not alert box) to proceed. In order to do that, I have to alert the activity class somehow. because I can't call setContentView(R.layout.insertwin); in my second class. somehow it must need to call in the activity class. how would I do that?
You can do your own listener interface in the View subclass and subscribe to this listener int the Activity subclass after calling setContentView(). So you'll be able to notify your activity about any events you want.
You can implement singleton to do such things

Categories

Resources