Question

I'm having a lot of fun playing with the Transfuse framework for Android, but I can't figure out how an Activity can return values through Intent.

Normally in an Android app, you'd call startActivityForResult to start an Activity and then onActivityResult will be called back when the started activity finishes and you are given an Intent, which contains returned data. I don't see how Transfuse handles this without totally breaking out of the framework and go legacy all the way. There's no event annotation for onActivityResult and IntentFactory doesn't seem to be able to start Activity with startActivityForResult So far this is a deal breaker for me.

While on this subject, another related question I'd like to raise is that, in Android, you could use the following code to rewind the Activity stack to a destination Activity.

Intent i = new Intent(this, DestActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("key", "val");
startActivity(i);

Then the destination Activity would get its onNewIntent callsed, which would get the Intent passed to it. Javadoc I don't see how this can be done with Transfuse. IntentFactory almost got this, except it can't set flags to Intent, and there's no event annotation for onNewIntent. I'd also like to verify that let's say I have the following.

@Inject @Extra("key") String key;

Will it be updated when onNewIntent is called?

Thanks in advance. I'd like to specially thank johncarl for his amazing work. I had a lot of fun.

Was it helpful?

Solution

Thanks for the kudos on Transfuse and the kind words.

It looks like the @OnActivityResult event mapping was missing from Transfuse, but it was easy enough to add (https://github.com/johncarl81/transfuse/issues/47). You will have to use the current 0.2.3-snapshot to use this functionality, which should be available on maven central, until a non-SNAPSHOT is released.

Transfuse does not handle the Activity results via injected extras and for the time being it looks like you will need to handle this the regular Android way. The good news is Transfuse can help map in the button clicks and intent building. Here's an example:

Button listener to trigger intent in the first Activity:

@RegisterListener(R.id.resultonebutton)
public android.view.View.OnClickListener listener = new android.view.View.OnClickListener() {

    @Override
    public void onClick(android.view.View view) {
        android.content.Intent intent = intentFactory.buildIntent(new ResultTwoActivityStrategy());
        context.startActivityForResult(intent, REQUEST);
    }
};

Button listener to return a result from the second Activity:

@RegisterListener(R.id.resulttwobutton)
public android.view.View.OnClickListener listener = new android.view.View.OnClickListener() {

    @Override
    public void onClick(android.view.View view) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra(ResultOne.RESULT_KEY, inputText.getText().toString());
        activity.setResult(android.app.Activity.RESULT_OK, returnIntent);
        activity.finish();
    }
};

Then the OnActivityResult method in the original Activity:

@OnActivityResult
public void result(int requestCode, int resultCode, android.content.Intent data) {
    if (requestCode == REQUEST) {

        if(resultCode == android.app.Activity.RESULT_OK){
            String result=data.getStringExtra(RESULT_KEY);
            Toast.makeText(context, result, SharedVariables.ONE_SECOND).show();
        }
    }
}

Don't forget that you can inject the current Activity, IntentFactory, and all Widgets.

To add flags to the intent, you simply need to build the intent and call the .addFlags() method. If you're using the IntentFactory it would look like this:

Intent intent = intentFactory.buildIntent(new ResultTwoActivityStrategy());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent, REQUEST);

It looks like the onNewIntent event is also missing. I will look at adding that soon along with any other recently added events.

OTHER TIPS

To handling the Result you can also do this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == YOUR_REQUEST_CODE_TO_CHECK) {
        if (resultCode == RESULT_OK) {
            // Do something.
        }
    }
}

have fun

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top