3

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.

4

2 回答 2

3

感谢您对 Transfuse 的赞誉和客气话。

Transfuse 中似乎@OnActivityResult缺少事件映射,但添加起来很容易(https://github.com/johncarl81/transfuse/issues/47)。您必须使用当前的 0.2.3-snapshot 才能使用此功能,该功能应该在 maven Central 上可用,直到发布非 SNAPSHOT。

Transfuse 不会通过注入的附加功能处理 Activity 结果,目前看来您需要以常规的 Android 方式处理此问题。好消息是 Transfuse 可以帮助映射按钮点击和意图构建。这是一个例子:

在第一个 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);
    }
};

按钮侦听器从第二个 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();
    }
};

然后是原始Activity中的OnActivityResult方法:

@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();
        }
    }
}

别忘了你可以注入当前的Activity、IntentFactory,以及所有的Widget。

要将标志添加到意图,您只需构建意图并调用 .addFlags() 方法。如果你使用 IntentFactory 它看起来像这样:

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

看起来 onNewIntent 事件也不见了。我将考虑尽快添加它以及任何其他最近添加的事件。

于 2013-07-20T22:21:33.197 回答
0

要处理结果,您还可以这样做:

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

玩得开心

于 2014-07-10T13:55:41.030 回答