Category: Android

How to register my own Application subclass in Xamarin.Android?

by admin
In Java We would add a line to the manifest and pass it the namespace and name of my application: In Xamarin, there is the [Application] attribute but the documentation states that the Name member is not supported. So how do I get my subclass registered? Where to apply the attribute to? If I add the attribute to my subclass, I get: System.NotSupportedException: Unable to activate instance of type Android.MyApplication from native handle 14d00019 After Reading so many blogs I have resolved it, I have Gone through the below official documentaion and found some important informations. There are two scenarios in which the (IntPtr, JniHandleOwnership) constructor must be manually provided on a Managed Callable Wrapper subclass: Android.App.Application is subclassed. Application is special; the default Applicaton constructor will never be invoked, and the (IntPtr, JniHandleOwnership) constructor must instead be provided. Virtual method invocation from a base class constructor. Note that (2) is a leaky abstraction. In Java, as in C#, calls to virtual methods from a constructor always invoke the most derived method implementation. For example, the TextView(Context, AttributeSet, int) constructor invokes the virtual method TextView.getDefaultMovementMethod(), which is bound as the TextView.DefaultMovementMethod property. Thus, if a type LogTextBox were to (1) subclass TextView, (2) override TextView.DefaultMovementMethod, and (3) activate an instance of that class via XML, the overridden DefaultMovementMethod property would be invoked before the ACW constructor had a chance to execute, and it would occur before the C# constructor had a chance to execute. This is supported by instantiating an instance LogTextBox through the LogTextView(IntPtr, JniHandleOwnership) constructor when the ACW LogTextBox instance first enters managed code, and then invoking the LogTextBox(Context, IAttributeSet, int) constructor on the same instance when the ACW constructor executes. Solution: [Application] public class MyApplication : Application { public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip) { } }

Difference between Parcelable and Serializable?

by admin
In order to develop an Android App, we all learn that we cannot just pass object references to activities and fragments, we have to put those in an Intent / Bundle. Passing primitive data types like string, integer, float, etc. through intents is quite easy in Android. All you have to do is put the data with unique key in intents and send it to another activity. If a user wants to send Java objects through intent, Java class should be implemented using the Parcelable interface. Serialization, on the other hand, is a Java interface that allows users to implement the interface which gets marked as Serializable. public class User { String UserName; String Reg; public User(String name,String reg){ UserName=name; Reg=reg; } } Above is the simple User class with three private fields and getters and setters. Here we can not pass objects of this class between activities/services. To do this we need to make this class parcelable or serializable. Here taking an example of Parcable public class User implements Parcelable{ String UserName; String Reg; public User(String name,String reg ){ UserName=name; Reg=reg; } //parceling part public User(Parcel in){ String[] data= new String[2]; in.readStringArray(data); this.UserName= data[0]; this.Reg= data[1]; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeStringArray(new String[]{this.UserName,this.Reg}); } public static final Parcelable.Creator CREATOR= new Parcelable.Creator() { @Override public User createFromParcel(Parcel source) { // TODO Auto-generated method stub return new User(source); //using parcelable constructor } @Override public User[] newArray(int size) { // TODO Auto-generated method stub return new User[size]; } }; }

How AsyncTask works in Android?

by admin
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask. How many AsyncTask can be started at a time in an Android app? Ans. The ThreadPoolExecutor has a corePoolSize, a maxPoolSize and a QueueCapacity. If all of your threads from the corePoolSize are used, any other task are put in the queue of each of the threads from the core pool. After these queues are full, a new thread is being initialized, as long as it is within the limits of the maxPoolSize.


CPU_COUNT = Runtime.getRuntime().availableProcessors();

MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; For more information you refer the source code of Async Note: It also depends on android api level.