Passing data to AsyncTask: execute(args) vs constructor? - android

in my projects I'm using lots of AsyncTasks. All they send some data via http to a server. In some of them I pass the data vie execute( data... ). In others I'm using constructor for that:
new SomeTask().execute( 1, 2, 3 );
or
new SomeOtherTask( 1, 2, 3 ).execute();
Basically those calls are equivalent, as the AsyncTask cannot be re-used and is stateless.
What is the preferable way to use those? Am I missing something?

In my opinion, pass parameters to the execute method (if they all share the same type, off course). Using a custom constructor forces you to write it and store parameters in private variables, which is useless, unless these parameters are used in the OnPostExecute method for instance.

Use new SomeTask().execute( 1, 2, 3 );
It is better than passing values to constructor. If you pass your input as argument to constructor, you will need to create member variables and that will be superfluous, since AsyncTask allow us pass variable arguments in execute method.
Until and unless there is need to maintain member variables for the input passed to AsyncTask, constructor should not be used to pass input.

Related

I can't pass the argument while using higher order function

Here you can see that I can't pass that string parameter
I somehow learned how to pass a method in the same class but, when I want to call it from another class, I can't pass the arguments.
Several issues here:
the introduceMyself method accepts a higher-order function, but in its usage it seems that you don't want the method itself, but its result. I'd suggest replacing the getName parameter with name parameter and convert it to a regular string which will be provided externally by an invocation of said method.
In line 6, you have a syntax error. Since you're using an instance - person, you can replace the :: with a . like so: person.myNameIs("michael"). It's not working because :: passes the method itself, which you don't really want. Again, your code implies that you need to pass the result of the method and not the method itself.
Finally, you can pass higher order functions, but you can't pass them "with an argument". You can do one of 3 things:
Invoke the HOF locally and pass the result of the invocation
Pass the HOF and the argument and invoke them wherever it is you need to.
Pass the HOF and let the target pass whatever argument it chooses.

How can we decide whether use to writeTypedList/ readTypedList or writeList/ readList

Currently, I still wondering when we implement Parcelable interface for a class, how can we decide when to use writeTypedList/ readTypedList or writeList/ readList
Read
// Using writeTypedList
parcel.writeTypedList(watchlistColumnTypes);
// Using writeList
parcel.writeList(watchlistColumnTypes);
Write
// Using readTypedList
watchlistColumnTypes = new ArrayList<>();
in.readTypedList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.ColumnType.CREATOR);
// Using readList
watchlistColumnTypes = new ArrayList<>();
in.readList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.class.getClassLoader());
Both workable for me. But, I'm not sure what is the difference between both, and how can we choose among them?
I think the rule of thumb is that, we should use writeTypedList or readTypedList whenever we could, and use writeList or readList whenever you need to.
This is because writeTypedList or readTypedList seems to have better performance.
According to https://android.googlesource.com/platform/frameworks/base/+/1a008c1/core/java/android/os/Parcel.java#117
There are also some methods that provide a more efficient way to work
with Parcelables: writeTypedObject(T, int), writeTypedArray(T[], int),
writeTypedList(List), readTypedObject(Parcelable.Creator),
createTypedArray(Parcelable.Creator) and
createTypedArrayList(Parcelable.Creator). These methods do not write
the class information of the original object: instead, the caller of
the read function must know what type to expect and pass in the
appropriate Parcelable.Creator instead to properly construct the new
object and read its data.

Methods : Make my method with many input variables with out overloading

is there Any Way To Make my Method Take many input variable but with out overloading ... could be my question not clear ... I mean Like That :
if I Have This Method
public void setValues (int val1,int val2 ,String val3){
}
what I want is : use this method with many way
setValues (val1,val2)
OR
setValues (val3)
why I want to do that with out overloading : Because if i have as example 10 variable i want to add many method with overloading but i don't like that ...
is there any way helps me to check variable or skip it in the same method ..
Thanks for help .
You can use varargs future to partially solve your problem http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
This can be done if you have parameters of a same type.
But it will require that you will pass variables with the same type as a last param. It is not completely what you want, but it is a small workaround.
No there's no way to do that in Java without method overloading. One alternative would be to group those parameters which are related and make them fields of a class. Then the method would take an instance of that class as a parameter, and which ever parameters were optional would then be null.

How to pass not basic data to activity on creation?

In every example I saw, the data is somehow synonymous to basic (raw) data -- ints, chars, array of bools, and so on -- this is too limiting for me, because I would like to pass a regular object.
So how to pass any data to activity, like for example, instance of MyClass?
I checked the Intent.putExtra -- all I found was basic types + Bundle, but Bundle itself also handles only basic types.
There are several way to do it as described in android guide faq.
I think that in your case static variables could help most.
You could also implement Application and use it to share your data between Activities.
Here is short tutorial on that.
In every example I saw, the data is somehow synonymous to POJO data -- this is too limiting for me, because I would like to pass a regular object (not int, or string, or array of bools).
POJO = Plain Ol' Java Object = "regular object (not int, or string, or array of bools)".
So how to pass any data to activity, like for example, instance of MyClass?
Make it Parcelable.

Android Parameter Passing Method

Which parameter passing method is used in Android? Pass by reference or Pass by Value? Please provide an Android Specific example for the same.
All parameter passing in Java is by value.
Here is a nice article with all the details.
just like a normal Java Application:
Objects are passed by reference,
primitives are passed by value

Categories

Resources