Android Open Source - rest_service_tutorial R E S T Responder Fragment






From Project

Back to project page rest_service_tutorial.

License

The source code is released under:

Copyright (C) 2012 Neil Goodman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softw...

If you think the Android project rest_service_tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.neilgoodman.android.restservicetutorial.fragment;
/*from w  w w . j  av  a2 s .c  om*/
import net.neilgoodman.android.restservicetutorial.service.RESTService;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.support.v4.app.Fragment;

public abstract class RESTResponderFragment extends Fragment {
    
    private ResultReceiver mReceiver;
    
    // We are going to use a constructor here to make our ResultReceiver,
    // but be careful because Fragments are required to have only zero-arg
    // constructors. Normally you don't want to use constructors at all
    // with Fragments.
    public RESTResponderFragment() {
        mReceiver = new ResultReceiver(new Handler()) {

            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultData != null && resultData.containsKey(RESTService.REST_RESULT)) {
                    onRESTResult(resultCode, resultData.getString(RESTService.REST_RESULT));
                }
                else {
                    onRESTResult(resultCode, null);
                }
            }
            
        };
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // This tells our Activity to keep the same instance of this
        // Fragment when the Activity is re-created during lifecycle
        // events. This is what we want because this Fragment should
        // be available to receive results from our RESTService no
        // matter what the Activity is doing.
        setRetainInstance(true);
    }
    
    public ResultReceiver getResultReceiver() {
        return mReceiver;
    }

    // Implementers of this Fragment will handle the result here.
    abstract public void onRESTResult(int code, String result);
}




Java Source Code List

net.neilgoodman.android.restservicetutorial.RESTServiceActivity.java
net.neilgoodman.android.restservicetutorial.fragment.RESTResponderFragment.java
net.neilgoodman.android.restservicetutorial.fragment.TwitterSearchResponderFragment.java
net.neilgoodman.android.restservicetutorial.service.RESTService.java