Skip to content

A library made to make asynchronous calls in android very easy

Notifications You must be signed in to change notification settings

SiebenGR/EasyAsync

Repository files navigation

EasyAsync

A library made to make asynchronous calls in android very easy

EasyAsync library is a library that makes asynchronous calls very easy and compact, minimizing the boilerplate code that a developer has to write. All the asynchronous calls are invoked inside an android.os.AsyncTask in a retained android.app.Fragment or a android.support.v4.app.Fragment

Acquire the object as a singleton EasyAsync.getInstance();

The EasyAsync interaction must be made inside a FragmentActivity or a Activity.

Firstly you have to initialise the object by using one of the following:

  • EasyAsync.getInstance().init(android.support.v4.app.FragmentManager);
  • EasyAsync.getInstance().init(android.app.FragmentManager);
  • Quick Example 1:

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
     EasyAsync.getInstance().init(getSupportFragmentManager());
    
     //...other stuff goes here
    

    }

    Quick Example 2:

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
     EasyAsync.getInstance().init(getFragmentManager());
     //you should start an asynchronous operation here
    
     //...other stuff goes here
    

    }

    In the android.app.Activity or android.support.v4.app.FragmentActivity class you have to create public methods that are annotated with the @BackgroundJob annotation and you must specify an id as a String. This method will be triggered in the different states of the AsyncTask. (Read the AsyncTask documentation here.) A @BackgroundJob annotated method must have zero parameters, one parameter of type EasyAsyncCallbacks or two parameters of types (EasyAsyncCallbacks, EasyAsyncResult).
    EasyAsyncCallbacks: indicate the AsyncTask states
    EasyAsyncResult: is an object that is used across AsyncTask to maintain the parameters and the results of the asynchronous calls

    Full Quick Example:

    
    @BackgroundJob(id = "demoid")
    public void demoAsync(EasyAsyncCallbacks callbacks, EasyAsyncResult args) {
         if(callbacks == EasyAsyncCallbacks.ON_BACKGROUND) {
             //do stuff in the background and set the result in the args object
             args.setResult(result);
         } else if(callbacks == EasyAsyncCallbacks.AFTER_EXECUTE) {
             //retrieve the result object
             String onPostResult = args.getResult();
             //do stuff on the foreground
         }
    }
    

    To start the background job you must use the EasyAsync.getInstance().start(String id) or EasyAsync.getInstance().forceStart(String id) method with the desired background job id that is used in the annotated method.
    Using EasyAsync.getInstance().start(String id) method the background job is executed once and then is cached in memory. If you start a specific background job again then the annotated method will be invoked like it has been executed.
    If a background job needs to be re-executed then call EasyAsync.getInstance().forceStart(String id), it will re-schedule the async task.

    NOTE: If the background job has already finished during an orientation change the annotated method will be invoked again as though it has just finished, for convenience. You can change this behavior by using EasyAsyncResult.setCallbackInConfigurationChange(boolean) method in the annotated method parameters.

    To avoid memory leaks you should invoke EasyAsync.getInstance().destroy(android.app.Activity) or EasyAsync.getInstance().destroy(android.support.v4.app.FragmentActivity) in the onDestroy() method of your Activity respectively.

    License

    
    Copyright 2014 SiEBEN Innovative Solutions
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
       http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    

    About

    A library made to make asynchronous calls in android very easy

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published