0

I have an android app with an activity (Activity A) that shows an image for 3 seconds (splash screen) and after that the main activity (Activity B) will start.

In my main activity I start a service to fetch some data from web but this work take some time and the user become uncomfortable.

What I want is to start the service from Activity A and send the result to Activity B to show some results to user. The problem is that I don't have an instance of Activity B in the Activity A to send the ResultReceiver instance to IntentService.

How can I do this?

I have a nested class in Activity B that extends ResultReceiver .

Hamidreza Salehi
  • 930
  • 2
  • 10
  • 22
  • follow this ..http://stackoverflow.com/questions/10334901/how-to-get-results-from-an-intentservice-back-into-an-activity – Radhey Feb 20 '15 at 14:09

1 Answers1

0

Sounds like you need a data model to hold the data between activities.

One solution would be to create a static class or use the singleton design pattern. This could take the result from the service. You would initialise this in Activity A and start the service before firing the intent for Activity B

Then in activity B you can send a call a method to register it's call back function. If the data is there come back straight away to the call back function. If not do so when your new class is called back from the function.

Your data is then shared and only fetched/refreshed as and when you need it to be.

// ----------- Class to hold your data Item -----------

   public class DataItem {

        private Integer id = -1;

        // My Data stuff
        public Integer getId() {
            return this.id;
        }
    }

// ----------- Interface to join it all together -----------

    import java.util.ArrayList;

    public interface NotifiyDataInterface {

        public void completedDataLoad( ArrayList data, String status);

    }

// ----------- Singleton Class for the datafetching -----------

    public class DataFetcher {

        static DataFetcher _instance = null;

        ArrayList<NotifiyDataInterface> _callBackList = null;
        ArrayList<DataItem> _cachedData = null;
        String _dataStatus = "";

        public DataFetcher() {
            _callBackList = new ArrayList<NotifiyDataInterface>();
        }

        public static DataFetcher getInstance() {

            if (DataFetcher._instance == null) {
              DataFetcher._instance = new DataFetcher();  
            }

            return _instance;
        }


        public void fetchData(NotifiyDataInterface _callBack) {

            if (_cachedData != null) {
                _callBack.completedDataLoad(this._cachedData, this._dataStatus);
                return;
            }

            // Add to the call back list
            this._callBackList.add(_callBack);

            // Code to call your service to get data
            //TODO: Add code to call your service

        }


        public void dataLoadComplete(ArrayList<DataItem> _newItems, String _fetchStatus) {
            // Called from the service on a completed data load

            this._cachedData = _newItems;
            this._dataStatus = _fetchStatus;

            NotifiyDataInterface _item = null;

            for (int i = 0; i < _callBackList.size(); i++) {
                _item = _callBackList.get(i);
                _item.completedDataLoad(this._cachedData, this._dataStatus);
            }

            // Clear out the call back list
            _callBackList.clear();
        }

    }

// ----------- Class for activity B -----------

public class ActivityB  extends ActionBarActivity implements NotifiyDataInterface {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        // Get the single instance from the singleton pattern
        DataFetcher dataFetcher = DataFetcher.getInstance();
        dataFetcher.fetchData(this);
    }

    // Here is where you call back is fired
    public void completedDataLoad( ArrayList data, String status) {
        //TODO: Your Code to call on data load here
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.layout.activity_b, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

JodiMiddleton
  • 310
  • 1
  • 8
  • What if the activity B created successfully but the service still running? I want something like ResultReceiver in activity B so whenever the service finished the receiver automatically fire. – Hamidreza Salehi Feb 20 '15 at 15:07
  • You will need to implement an interface that has a guaranteed function. Pass in Activity B as a reference. The new class will call you function when data is available. If data is already available you get an instance call back and if not it will do it when it comes available. If you like I can knock up a little code to show you? – JodiMiddleton Feb 20 '15 at 15:10
  • yeah i appreciate it. – Hamidreza Salehi Feb 20 '15 at 15:13
  • Code added. I haven't tested it but shows the idea of having a shared Model/data handler between the classes. You can call the same DataFetcher.fetchData from Activity A and just ignore the results there. – JodiMiddleton Feb 20 '15 at 15:49
  • Thanks. I will look into It. and let you know – Hamidreza Salehi Feb 21 '15 at 05:04