Search code examples
androidweb-servicesandroid-fragmentsandroid-tabs

Best way to display web service data in Tabs fragments in Android


I need to develop an Android app with the following screens to be achieved:

  1. Login Activity (here on click of Login button, need to call a web service which fetches list of huge data from server for 3 different products say, A, B and C, probably in 3 separate ArrayLists)

  2. List Activity (here I need to display the ArrayLists' data in 3 different tabs for A, B & C)

  3. Details Activity (after clicking on any particular list item in any tab A/B/C - the details of that particular item should be displayed)

What is the best way to achieve the 2nd point? Can we render all the tabs data at the same time - because I don't want to start new web request on click of other tabs.

Could you guys please help me.


Solution

  • My solution

    1-Singleton to keep 1 istance of your data 2-Service store data in singleton 3-In onCreateView of Fragments keep data from singleton ad display data

    This is my solution for your 2nd point

    For 3rd point, you can implement serializable in your object and pass it with an intent from your listActivity to your DetailActivity (in listActivity it's easy get the object in Array by position)

    EDIT

    SINGLETON

    public class GlobalVarSingleton {
    private static final String TAG = "GlobalVarSingleton";
    private static GlobalVarSingleton mInstance = null;
    
        private static myObj token;
    
        private GlobalVarSingleton(){
    
        }
    
        public static GlobalVarSingleton getInstance(Context ctx){
            if(mInstance == null)
            {
                Log.d(TAG, "init Global Singleton");
                mInstance = new GlobalVarSingleton(); 
            }
            return mInstance;
        }
    
        public void setData(myObj data){
            this.token = data;
        }
    
        public myObj getData(){
            return token;
        }
    }
    

    To access -> GlobalVarSingleton.getIstance(context).setData(data); (or getData...) this allow you to have a single istance of your object in app. The best it's init this Singleton in Application class

    public class MyApplication extends Application {
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        GlobalVarSingleton.getInstance(this);
    }
    }
    

    So in Service you just need call setData it as up and pass your data just getted. myObj is a sample, you must use your data object offcourse.

    In CreateView with GlobalVarSingleton.getIstance(this.getActivity()).getData(); you get data storaged

    For serializable look it Passing data through intent using Serializable

    Note, it's important that you know when data is available, I think that you wait web response to load new listActivity, is it right?

    ABOUT APPLICATION CLASS http://developer.android.com/reference/android/app/Application.html