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.
Related
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.
After the ADT update in eclipse whenever I create a new project for android a default fragment activity is created. How to get to create simple activity always?
I don't want to downgrade the ADT.
Is this possible? any work around?
The latest ADT plugins are made in such a way that if you create any simple project you will always get the FragmentActivity not a simple Activity. And you can not change it.
In new ADT itself there is Blank Activity that is defined with an actionbar and optional navigational elements such as tabs or horizontal swipe.
If you want to create simple activity in your project then you can simply create class and extends Activity into it, this is the only way now. Or you can change the Fragment with Activity simply by changing code only.
As an Activity is simply a Java class, just accept the defaults the ADT gives you during the initial creation of your project. Create your project, then manually:
1. Delete all the class and layout files in your project.
2. Create your Java class files extending Activity (in your desired packages).
3. Create XML layout files (in the res/layout folder).
4. 'Wire-up' your Java class files and XML layout files using setContentView(R.layout.YOUR_LAYOUT_ID) in the onCreate() methods.
5. Done!
EDIT
Remember to Register your Activities in the Manifest manually as well.
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.
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!
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.