2

Is it possible in Android application to get rid of Intent system completely and use only event bus (Otto, greenrobot)? For example, is it possible to achieve this with event bus only, without Intents at all:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

If I use event bus, will it automatically resume correct activity and bring it to front, as it is done with Intents?

afrish
  • 3,167
  • 4
  • 30
  • 38
  • nice q! +1 wondering the same exact thing, changing the activity is my only hang up...how to automate that with out intents.... – sirvon Jul 28 '14 at 23:26
  • @sirvon while no other answers given, see my own observations below – afrish Jul 29 '14 at 05:55

1 Answers1

3

The only solution I've found so far is a set of utility methods, that is a mix of Intents and EventBus from greenrobot:

public class ActivityUtils {
    public static void startForResult(Activity context, Class<?> destinationActivity, Object param) {
        Intent intent = new Intent(context, destinationActivity);
        EventBus.getDefault().postSticky(param);
        context.startActivityForResult(intent, 0);
    }

    public static void returnSuccessfulResult(Activity context, Object result) {
        Intent returnIntent = new Intent();
        EventBus.getDefault().postSticky(result);
        context.setResult(Activity.RESULT_OK, returnIntent);
        context.finish();
    }

    public static <T> T getParameter(Class<T> type) {
        return EventBus.getDefault().removeStickyEvent(type);
    }
}    

In my FirstActivity I call somethig like:

ActivityUtils.startForResult(this, SecondActivity.class, new MyParam("abc", 123));

After that in the SecondActivity I call:

MyParam param = ActivityUtils.getParameter(MyParam.class);

When I finish the SecondActivity:

ActivityUtils.returnSuccessfulResult(this, new MyResult("xyz"));

And then in the FirstAcivity:

MyResult result = ActivityUtils.getParameter(MyResult.class);
afrish
  • 3,167
  • 4
  • 30
  • 38