How to extend 2 classes? - android

This might be a silly question, but I'm struggling on how to make my class extend 2 classes at the same time. I'm trying to make a SERVICE that uses ListActivity. How could I do that?

Java or android Doesn't Support Multiple Inheritance

I assume you are coding in the Java programming language. Then the simple answer is: You don't. Java does not support deriving from multiple classes.
Make your ListActivity contain a Service.
class MyService extends Service{
...
}
class MyList extends ListActivity{
MyService service = new MyService();
}

In Java you can't extend more than 1 class at the same time. However, you can implement more than 1 interface (but this is not about your task).
You should look for bound service

No, it is not possible. You may need to re-architect your implementation.

You can never actually extend 2 classes directly, you need to use a few tricks and make a few interfaces to get it to work. Here is a guide: http://csis.pace.edu/~bergin/patterns/multipleinheritance.html

Assuming you mean this kind of Service, you cannot do this at all; irrespective of Java's lack of multiple inheritance. Services cannot also be Activities. You should create a Service, and a separate ListActivity, and use a binder to communicate between them.
When you do want to inherit the functionality of two classes, use the delegation pattern.

Related

Dagger - Hilt : Do we need to mark all activities with #AndroidEntryPoint

So my question is as I am getting started with Hilt, do we need to mark all activities with #AndroidEntryPoint annotation or can we just create a BaseActivity and extend it to AppCompactActivity and mark that single class as the entry point?
Will this work? and what, if any, are the drawback of this style.
Thanks.
I'm not sure if this is a comprehensive answer, maybe it's more of a personal opinion, but I'd ask what's the goal?
Are you trying to reduce boiler plate/amount of code you need to write? Then I'm afraid it'll be the same if not more, since you'll have to go to every activity and add the inheritance part.
The downside to me is that you're now using inheritance to implement something that was avoiding it. Annotations give you the opportunity to annotate any activity without saying it's a base activity. This is often better than inheritance since not every activity is a base activity and you decouple your code more from what dagger is actually doing. I think this is more of a delegate pattern or maybe more like a decorator.
That said, to answer your question, I'm not 100% this works, but to me it has the downsides of using inheritance for something that should not be modelled by inheritance.
You cannot mark a "base class" as an AndroidEntryPoint if it is abstract, so this idea would not work anyway. As there is usually one Activity per App (recommended by google), you should not make your life harder than it is. Just annotate an Activity with #AndroidEntryPoint and you are done here.
as the document says:
Classes that Hilt injects can have other base classes that also use
injection. Those classes don't need the #AndroidEntryPoint annotation
if they're abstract.

Which one is better option for maintaining application or activities?

Which one is better option for application or activities?
A. BaseActivity class, [OR]
B.Singleton class.
In my application most of activities do same actions like show toasts, maintaining sessions, static variables,show EditText errors, HTTP request/response, etc., For now I'm extends BaseActivity in all my Activities where needed.
You're on the right path, a BaseClass is preferred because most of those thing you mentioned (Manipulating Toasts, EditText functions) require a Context to work with. A Singleton class would need that Context passed to it with every method call - a BaseClass does not (since it is technically a Context itself).
These are two fundermental different design patterns.
BaseActivity --> Inheritance
Use a Singleton in all activities --> Aggregate pattern
You find many posts in the internet about advantage and disadvantage of both patterns.
For your specific problem I would suggest to use inheritance. It is a common way to do and you should avoid to use the singleton pattern if possible.

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.

Multiple activities within a class?

So what I want to do is this. I want to have a class that contains other classes which start activities, but I'm not sure if its possible, or even a good idea. An Example:
public class General{
public class Activity1 extends Activity{
//Start Activity
}
}
Is there a way to call such an activity?
So the solution I chose to go with was to use packages. After reading about it here http://developer.android.com/guide/topics/manifest/manifest-intro.html and here Android: Including multiple Java Packages to Manifest , it seems like a better method to do what I stated above.

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