Can I have an Activity extend my Activity - android

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...

Related

Android Root of all Activities and Fragments

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.

How to handle Android Java objects which "extends Activity"?

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.

Android and extends about two activities

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

How to use multilevel inheritance in android

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.

How to start activity from non-activity class,if first activity is simple java class?

I want to start activity from 1st java class that extends BroadcastReceicer class.so i can`t extends activity class to start activity of another java class by using startactivity method in 1st class.
Give me proper guidance what should i do now?
use context in the Onrecieve function of the broadcastreciever,
context.startActivity(...);

Categories

Resources