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.
Related
the clip of the code written for using voice interaction which is used in another Java file I have already tried importing android.app.VoiceInteractor but its not working
You may have imported all necessary classes in your code. As your snippet you forgot to extend that, But you are already extending AppCompatActivity so that you can't extend another class in the same Activity. What you need to do is create another class inside MyVoiceActivity and extend VoiceInteractor.ConfirmationRequest in that class.
Your Activity will look something like this
public class MyVoiceActivity extends AppCompatActivity{
class Confirm extends VoiceInteractor.ConfirmationRequest {
//All method you wanted goes here
}
}
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.
I cannot decide what approach should I use in the next situtation.
One activity from my app need to have different functionality, here is the leak of multiple inheritence comes into play.
What I need to get ?
There are several parts that my activity has to have.
Navigation drawer. I am using MaterialDrawer library. It requires activity to implement on click callbacks (or use composition instead), but also it use activity as constructor argument,so I think it will be better to put this into separate class inherited from Activity or any base class provided by Android Framework . Thanks to library developer it doesn't require any stuff to be done in on create method ( setContentLayout for instance)
My activity(one of several) will have only Toolbar and FrameLayout for holding fragment . Or it is better to separate toolbar and single fragment ?
This in turn requires some stuff to be done in onCreate : setContentLayout with basic layout , add fragment to the container set activity actionbar .....
Maybe in future I will use another libraries that requires to add something in activity lifecycle methods.
All these points in order to follow Single Responsibility principle have to be separate classes inherited from some base Activity.
For example we will have something like this.
public class SingleFragmentActivity<T extends Fragment> extends AppCompatActivity {
}
public class SingleFragmentToolbarActivity<T extends Fragment> extends AppCompatActivity {
}
public class NavigationDrawerActivity extends AppCompatActivity {
}
....
As you can see each functionality is put into the separate class, but what if I need to have SingleFramgentActivity with NavigationDrawer ?
In this case I have to inherit one of these classes from another, for example
public class NavigationDrawer extends SingleFragmentActivity {
}
If to do so, I have no ability to use navigation drawer separately from SingleFragmentActivity.
What is the best practice to follow in this case, how to build class hierarchy to make it flexible and use Open-Close principle, to make changes in application without any pain. (Use strategy, decorator ..... ?)
I would be grateful everyone for help.
I'm new to android programming how to extend two activity . In my case I'm using ActionBarActivity
im already extends the a class for some functionalities how shall i extend two activities my class
any example code will be more useful for me
This is not much of a Android problem (as Activity is class as any other), it's the way Java works.
Java doesn't support multiple inheritance, so classes can only extend one other class.
class A extends B{
}
Even in this case:
class A {
}
class A extends another class - Object - but it is automatically implied without having to specify it.
If you want to ensure some functionality from several sources you will have to use interfaces and the implements keyword:
class A extends B implements C,D,E {
}
I have created an abstract class which extends the Activity Class.
I want to extend it in an activity which extends TabActivity Class, how can I achieve that?
Get the TabActivity and abstract class Functionality at the same time.
I do realize that multiple inheritance is not possible in Java, but can I avoid extending TabActivity Class and still use getTabHost()?
10x :)
I don't think you can achieve this with this design. If your abstract class extending SomeOtherActivityClass and new Class need to extended SomeotherActivityClass (through inheritence) and TabActivityClass, that may confuse dalvik to choose which activity at runtime and it doesn't make sense. You may need to reconsider your design.
yes sure, you can use. By using following snippet:
TabHost host=(TabHost)findViewById(id of tabhost);
It's better way to create another class that extends Application class. Or use singleton pattern and create class with all functionality that you're need.