Use different (AsyncTask) Loaders in one Activity - android

I want to use different AsyncTaskLoaders (different in their return type) in my Activity, what's the best way to implement the callback methods?
This won't work:
public class MyActivity extends Activity implements
LoaderManager.LoaderCallbacks<MyPojo>,
LoaderManager.LoaderCallbacks<MyOtherPojo>
Eclipse says
The interface LoaderCallbacks cannot be implemented more than once with different arguments
So what do I do? My idea is to make the Activity
implements LoaderManager.LoaderCallbacks<Object>
then check in the callback methods what type of object it is but that doesn't seem too elegant. Is there a better way?

What about creating an inner class for each callback?
public class MyClass extends Activity {
private class Callback1 implements LoaderManager.LoaderCallbacks<MyPojo> {
...
}
private class Callback2 implements LoaderManager.LoaderCallbacks<MyOtherPojo> {
...
}
}

Related

Static method of singleton class used in multiple activities

There is an asynckTask and 2 methods,which are being called by 2 activities.
i Want to keep the AsyncTask class and the methods inside myApplication class
http://developer.android.com/reference/android/app/Application.html
( which was needed anyway,had some states of app to be maintained).
One other way is to have those methods in each activity and the asyncTask as independent class.
what is the best way?
How about having a base activity class for that?
Something like:
public class BaseActivity extends Activity {
protected void myMethod() {
// do what ever
}
}
Then just extend this BaseActivity to have that method in your activities.

can a singleton class extend Activity

I am using a singleton class to store global variables for the entire project. Also, to host some common functions which several classes/Activities may use, such as launching an alertBuilder window. But in order to do that... I need my singleton to extend Activity like this:
public class dataBaseObject extends Activity {
I tried to extend application, but that won't allow me to do this:
View view = context.getLayoutInflater().inflate(layoutType, null);
therefore, can someone tell me if there are any hidden pitfalls of extending Activity for a singleton ?
It doesn't make sense for an Activity class to be a singleton, because instances of Activity are instantiated by the android system.
What you can do is make an abstract class that extends Activity, like this
public abstract class AbstractActivity extends Activity {
public static final int EXAMPLE_CONSTANT = 345;
public final void exampleMethod() {
...
}
// This may not be needed
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
....
}
}
Then you can make all of your activity classes extend AbstractActivity. You do not need to declare an abstract class like this in manifest.xml.
An alternative solution is to make all of your utility methods have a parameter that is an Activity or a Context and pass this to these methods.

how does implement multiple types asynctaskloader

I want to use two type asynctaskloader in one FragmentActivity.
class MyLoader1 extends AsyncTaskLoader<String>{}
class MyLoader2 extends AsyncTaskLoader<Integer>{}
I write as follows. but it compile error.
public class MyActivity extends FragmentActivity
implements LoaderCallbacks<String>, LoaderCallbacks<Integer>{}
Please show me answer with easy sample code.
Thanks so much.
As hjpotter92 mentions, this is how Java handles generics. In this case, I would just suggest using anonymous classes as indicated in hjpotter92's link.
public class MyActivity extends FragmentActivity {
private LoaderCallbacks<String> mLoaderCallbackString = new LoaderCallbacks<String>() {
...
};
private LoaderCallbacks<Integer> mLoaderCallbackInteger = new LoaderCallbacks<Integer>() {
...
};
}
Then for each loader, you just pass the correct LoaderCallbacks object

Common Code for Activity and PreferenceActivity

I'm using common code in my Activity like this:
abstract class CommonCode extends Activity {
//Common Code here...
}
then in my "Activity" I extend CommonCode instead of Activity and it all works fine.
My problem arise when I try to use commoncode in a PreferenceActivity, I tried:
abstract class CommonCode extends Activity {
class CommonCodePreferences extends PreferenceActivity {
}
//Common Code here...
}
but it isn't right.
How can I do it?
May I suggest that you prefer composition over inheritance and do something like this:
abstract class CommonCode {
Activity parent;
public CommonCode(Activity activity) {
parent = activity;
}
}
class MyActivity extends Activity {
CommonCode commonCode;
public MyActivity() {
commonCode = new CommonCode(this);
}
}
This is a little more code to write in each activity, but it has a lot of advantages:
It can also easily handle PreferenceActivity and other classes
It is easier to test and mock
I usually have one each since you can't mess with the existing hierarchy of the base classes.
For example, I have an ActivityBase, ServiceBase, ListActivityBase, etc. If you want to have common code that they all use, I would suggest using composition - each of your base classes has a single instance of your CommonCode class or something to that effect. Another possibility is to use static methods and/or use a custom Application class (requires declaring the custom Application class in the manifest in the name attribute of the application element)

how to call method in service in android

I have created a service for TweetCollectorService. I want to call a method of another class in my service.can I do this Plz Help me.
Thankyou
Yes you can.. Only difference here is the method execution also occurs in background process.. No other difference..
Its not best practice to have other utility functions/methods in Activity, which mainly is to handle user interaction. so Strictly follow java convention and create different class which has all these methods, so it achieves cohesion.
You can dosomething like below..
Class YourActivity extends Activity{
public void do(){
// do your task
}
and in service just say new YourActivity().do()
}
Yes you can able to call the other class method. If you using your custom class then create the method as static so no need to create object of that class
suppose your custom class which extend Activity or not then also you can do like this way.
class CustomClass extends Activity{
public static void mymethod(){
// call me
}
}
now you can call into the service like this way without creating any object as explicitly
class MyService extends Service{
onCreate(){
CustomClass.mymethod();
}
}

Categories

Resources