Extending Android Classes In Eclipse - android

From what I can tell, Eclipse only supports making "skeleton-code" for very few class extensions in Android (such as Activity). If I want to extend TextView, Fragment, etc. I have to start completely from scratch and provide my own skeleton code.
Is that true, or am I missing something simple in Eclipse that creates skeleton code for various class extensions?

If by skeleton code you mean overriding the methods in the super class, then try the following:
In the package explorer, right click on the class, go to the Source tab and select Override/Implement Methods.... This will give a list of the methods you can override and implement.
Alternatively, go to a new line in the class editor outside of an existing method and press control + space to bring up a list of methods you can override. This is faster when overriding only one or two methods.

There is no difference between Activity or TextView when creating a new class from the Eclipse wizard.
By default all methods that need to be implemented will be auto-generated (constructors, abstract methods, interfaces' methods)

Say you class extends TextView.
Eclipse will ask you to override certain required methods in your class.
If you want to override some additional methods.
Right Click on the class name TextView. Goto Source and select Override/Implement methods. Then select the methods you want to override in your own class.

Related

What is an AbstractActivity in android?

What is an Abstract Activity in android? This question was asked at one of the interview. I tried searching about this at androidxref unfortunately not able to found.
Can any one help to answer this , Thanks!!
There is no such term in Android system called AbstractActivity.
Abstract activity is any activity which has been marked as abstract.
And like any other abstract java class, it cannot be instantiated and hence it will fail if passed as intent to startActivity() method. Also Android Studio will not allow you to declare such activity in the manifest file.
These abstract activities are mostly used by some android libraries to declare abstract methods and provides any method implementations useful for its task like login mechanism.
One of the advantage of this approach over an interface is that it can make use of activity callback methods.
As in comments the purpose of AbstractActivity is same as abstract class in java, which cannot be used directly(direct instance creation is not possible).
Using abstract activity you can define a group functionality for app activity screens.
For example,
LoginScreen: Abstract activity holding some functions defined
UserLoginScreen: specific ui/function for common user
AdminLoginScreen: specific ui/function for admin user

How do I extend two classes

I have this android application and I am trying to extend two classes at the same time. I have this code:
public class TimelineFragment extends Fragment {
public class TimelineFragment extends Activity {
//all codes here
}
}
On my second TimelineFragment, it has an error that says: The nested type TimelineFragment cannot hide an enclosing type
I have this android application and I am trying to extend two classes at the same time
That is not possible. Java does not support multiple inheritance.
I have this code
Given your class name is TimelineFragment, one presumes that it should extend Fragment. Whatever problem you are trying to solve via multiple inheritance will need to be solved in some other way. For example, if you are trying to perform operations on the activity that hosts your fragment, you can call getActivity() from the fragment.
Multiple inharitance is not possible in Java, and therefore in Android.
multiple inheritance is not possible in java so therefore u cant't use it in android as well.
Well one thing you can do is to create interfaces instead of class and implement any no. of interfaces you want and their methods.

Share Same code in Activity and ListActivity

My project consists of couple of activity and ListActivity items, there is some common piece of code(Navigation bar and some other codes) which needs to be done on both type of activity.
Is there a way I extend the activity and write my piece of code, and let ListActivity also inherent that code ?
right now I am copying the same piece of code in two classes , one is Activity extended and other is ListActivity extended
You could also have the common code in a class CSuperCommon, and have each of your Activities contain an inner class that inherits from CSuperCommon. Some initialization will need to be done such as setting the parent view, context, etc.
There is no real multi inheritance in java (and so in android) but it is possible to simulate it: http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html.
Here is another answer from stackoverflow: How do Java Interfaces simulate multiple inheritance?
Have both extend a Base Activity class (which will have your common code) and implement a list view in one of them. Implementing a list view is very easy!

Android Menu Codes in separate Class file

In my android application option menu should be available in every UI. So I need to separate those codes to a separate class file instead of repeating same code in every Activity. How can I do this in Android ?
One way is to inherit from a custom class that itself derives from 'Activity'. In this base class, you have the common code for the options menu.

Android: Using methods from an Activity in a Widget. Extend Activity and AppWidgetProvider?

I'm working on an Android app which has an activity and a widget. This is currently implemented via two classes in two .java files - one for the activity (extending Activity), one for the widget (extending AppWidgetProvider). Nothing out of the ordinary here as far as I'm aware...
However, the widget class code could be a lot simpler if it was to make use of functions and asynctasks defined in the activity class. Duplicating these functions seems like bad design, so I'm wondering how I can structure the app to make them usable?
Can I extend both Activity and AppWidgetProvider somehow? Can I import one in the other?
Thanks!
either make the funcs static, or make a 3rd class to hold these funcs
Move the functions down into a service. Create a Service and you can use context.startService(Intent) from you WigetProvider or from the activity to access the functions.

Categories

Resources