I have a problem. How I can extends for two activities. I have a activity which must extends two other activities, but I can't extends about two activities. How I can do this another way?
I need this because one activity extends Activity, but in this activity must extends MapActivity too because I'm using mapView.
In java you can not extend two classes at the same time. There are several approaches:
You can have one of the classes as a member field (lets call this A) and the other one (lets call this B) to be extended. Thus you will have access to the the private and protected methods of B and to all public methods of B. After all the main reason of extending an Activity is to get all Context's methods. From then on your own activities are just adding up methods. However, you can use them even without inheriting.
If it is possible you can make A extend B and then make your new activity extend only A. Thus all the methods of both activities will be inherited.
EDIT After your edit I can say you are already in case 2 of my explanation. MapActivity is an Activity by itself. You need not extend both. Extend just MapActivity.
MapActivity extends Activity, so you just have to inherit from MapActivity.
class YourClass extends MapActivity
Related
How can I implement "dispatchTouchEvent(...){}" on root of my android app instead of each of activities, because I want to get when my application get sleep or in pause state.
You could create a BaseActivity where you implement it and all your activities inherit from BaseActivity:
class BaseActivity extends AppCompatActivity {
dispatchTouchEvent() {...}
}
class MyActivity extends BaseActivity {
...
}
This kind of central handling would be possible if all your activities are extended from single activity in your project. Say, AppBaseActivity contains most general functionality & all others extend it. So, events can be traced down to this AppBaseActivity. I can't predict any other way to achieve the result you want to get.
Let's say I have two classes:
Class Show is my normal Android activity.
Class Work is a Java class which does a lot of work:
public class Work extends Activity {...}
As you can see, Work extends Activity. Thats because it needs some methods that are only in Activity (I don't mean methods which regard the UI).
In my Activity Show I make an Objekt of class Work
protected void onCreate(){
Work mWork = new Work();
mWork.doSomething();
}
My question:
How shall I handle my object Work regarding its lifecycle? Is it like a normal Java object and I don't have to care about its lifecycle, or is it like a normal activity and i have to call finish()? I am confused because it's kind of both.
Based on our discussion in the comments, the answer here would be to pass the Context of your current activity to the target class, and use the accessible methods through that context instance to do whatever task it is you want, as the Activity class basically extends ApplicationContext.
For example (in a class not extending Activity) I can add:
myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
and use myLocationManager as I please. Also, this way the object created locally would be set for garbage collection upon destruction of your main Activity.
Thought it'd be worth having an answer others could refer to rather than scrounging through the comments.
you should read this to understand activity lifecycle.
I have created an Activity which allows a user to create a page. This PageActivity extends an Activity. Can I create a class called Update PageActivity that extends PageActivity?
Yes, you can, as long as you did not declare PageActivity a final you may extend it.
Of course you can.
Activity is just like any other Java Class.
Make sure you override the relevant Activity lifecycle functions - onCreate, onResume, etc...
I have my application code base with multiple activities in it. I have a BaseActivity class which extends Activity class and is the parent class for all Activities in my application. BaseActivity takes care of the state my application goes in background and comes back to foreground.
Now I have written few more Activities which are using fragments so these new Activities are extending FragmentActivity. But my application design requires that all activities should extend BaseActivity class.
Solution in my mind:
BaseActivity extend FragmentActivity instead of Activity class.
New activities(with fragments) extend BaseActivity instead of directly extending FragmentActivity.
With this solution I am afraid of any side effect in my existing activities which were extending Activity class (through BaseActivity). Now these activities will extend FragmentActivity (although they are not having fragments inside).
Is it okay if I extend FragmentActivity even though I dont have any fragment inside. Will it behave same as Activity?
as FragmentActivity extends Activity so looks it's fine.jsut you need to add compitiblilty library if want to give the support for old versions
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
Even I did same and found no side effect yet
I am making an app in which i have to run an activity in background until the app is running.each activity related to this app is using first activity.how can it possible?
can i use the inheritance for this?
can anyone tell me any example of multilevel interitance in android?
You can create a BaseActivity class that extends Activity and all other Activities will extend this BaseActivity. Then what ever happened in all other activities (like resume and pause) will also effect the actions of BaseActivity.
If you have to accomplish background task you better to see android service
You are already extending the Activity class in a base class, and again you are extending this base class in other class. This is itself an example of multilevel inheritance. I am posting an example that may be relevant for your question:
public class basecls extends Activity{
/*The base class*/
}
public class secondcls extends basecls{
/* basecls extended by secondcls */
}
You can extend the secondcls in another class, and the new class will inherit all super classes such that you can use the methods of its super classes.
Your main activity is already using inheritance, since it extends the Activity class.