替代startActivityForResult [英] Alternatives to startActivityForResult

查看:136
本文介绍了替代startActivityForResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:改变片段部分,我是懵懂的片段对象时​​,我写这个

我有一个部分,它包含一个按钮,弹出联系人列表。这样做,需要调用

  startActivityForResult(新意图(Intent.ACTION_PICK,Contacts.CONTENT_URI),MY_REQUEST_ code);

和处理结果在我的活动,是这样的:

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
        开关(要求code){
            案例MY_REQUEST_ code:{
                地址地址= contact_address(数据);
                如果(地址!= NULL){
                    //做一些地址
                }
            }打破;
        }
    }
}

根据我怎么包括部分在我的活动布局,也可能是几层其他谐音,有可能是局部的多个实例。

我想,以避免传播MY_REQUEST_ code的ID一路下跌到调用该活动的部分 - 或其任何变化,比如分配一个onClickListener的按钮 - 我不想顶层UI关心如何分在所有的构造。

是否有实现这一目标的标准方法?在我看来,如果可以的onActivityResult作出接受的URI的不是int codeS,本来是可以避免的传播。我希望我缺少明显的东西在这里...


解决方案

  

我有一个包含一个按钮,调出联系人列表中的片段。这样做需要调用[ startActivityForResult()]


最简单的回答是给你打电话 startActivityForResult()上的片段不是活动。其结果应直接回片段自身的onActivityResult()

话虽这么说,你的方法似乎在什么,我建议几乎完全反转。碎片不应该首发活动。片段应该不知道,也不关心如何在特定的UI事件(如按钮单击)为东西都是片段本身之外处理。在这种情况下,该活动应负责取得联系,与片段只是告诉活动哎,这个按钮在这里GOTS pressed,哟,并给我一个接触回来。不仅是分离关注这个重要的,但它是重要的测试,因此您可以测试与嘲笑接触的选择和这一行为。


  

根据我怎么包括我的活动布局片段,它可能是几层其他的UI片段


您可以在Android的碎片不能嵌套片段。 如果你尝试,你会得到不可靠的结果


  

我不想顶级UI关心碎片如何在所有构成。


活动绝对必须知道这个片段是如何构造的,因为活动是一个做的构建。

即兴,这里是我会怎么对待它:


  1. 建立由超越片段本身的边界片段引发UI事件监听器接口。对于这个答案的目的,我会打电话给这个 OnFooEventListener


  2. 在活动创建/配置片段,活动提供了一个 OnFooEventListener 实例的片段。这可能是活动本身,如果活动实现了接口。


  3. 建立异步接一个接触事件,该片段希望有人代其执行监听器接口。对于这个答案的目的,我会打电话给这个 OnContactPickedListener 。它会像 onContactPicked()的方法包含选定联系人的乌里


  4. OnFooEventListener ,有一些像 requestContact()当用户点击,将调用按钮。 requestContact()将采取 OnContactPickedListener 实例作为参数。<​​/ p>


  5. 该活动将做 startActivityForResult()呼叫,并在的onActivityResult(),请致电相关 OnContactPickedListener onContactPicked()方法。该活动将缓存那些在的HashMap 或东西,而请求是过程。


现在,我们有活动与片段之间的清晰的分离。该片段仍然可以通过任何数目的活动被托管(例如,一个用于一个大的屏幕,不同的一个用于正常屏幕)。接触可提供或者由生产资料( ACTION_PICK ),或别的东西进行测试(例如,价值确立为测试案例的一部分)。活动可以处理任何数目的碎片没有问题。

Edit: changed Fragment to Partial, I was ignorant of the Fragment object when I wrote this.

I have a partial that contains a button to bring up the contact list. Doing this requires calling

startActivityForResult( new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), MY_REQUEST_CODE );

and handling the result in my Activity, something like:

public void onActivityResult( int requestCode, int resultCode, Intent data ) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case MY_REQUEST_CODE: {
                Address address = contact_address( data );
                if (address != null) {
                    // do something with address
                }
            } break;
        }
    }
}

Depending on how I include that partial in my Activity layout, it may be several layers deep in other partials, and there may be more than one instance of the partial.

I would like to avoid propagating the ID of MY_REQUEST_CODE all the way down to the partial that invokes the activity - or any variation thereof, like assigning an onClickListener to the button - I don't want the top level UI to care about how the partial is constructed at all.

Is there a standard way of achieving this? It seems to me that if onActivityResult could be made to accept Uri's instead of int codes, the propagation could have been avoided. I hope I am missing something obvious here...

解决方案

I have a fragment that contains a button to bring up the contact list. Doing this requires calling [startActivityForResult()]

The simplest answer is for you to call startActivityForResult() on the Fragment instead of the Activity. The result should be directed back to the fragment's own onActivityResult().

That being said, your approach appears to be a near-complete inversion over what I recommend. Fragments should not be starting activities. A fragment should neither know nor care how a particular UI event (e.g., button click) is handled for things that are outside the fragment itself. In this case, the activity should be responsible for obtaining the contact, with the fragment simply telling the activity "hey, this here button gots pressed, yo, and gimme a contact back". Not only is this important for separation of concerns, but it is important for testing, so you can test this behavior with mocked contact selections and such.

Depending on how I include that fragment in my Activity layout, it may be several layers deep in other UI fragments

You cannot nest fragments in fragments in Android. If you try, you will get unreliable results.

I don't want the top level UI to care about how the fragment is constructed at all.

Activities absolutely must know how the fragment is constructed, since the activity is the one doing the constructing.

Off the cuff, here is how I would approach it:

  1. Establish a listener interface for UI events raised by the fragment that transcend the bounds of the fragment itself. For the purposes of this answer, I'll call this OnFooEventListener.

  2. When the activity creates/configures the fragment, the activity supplies a OnFooEventListener instance to the fragment. That could be the activity itself, if the activity implements the interface.

  3. Establish a listener interface for the asynchronous pick-a-contact event that the fragment would like somebody to perform on its behalf. For the purposes of this answer, I'll call this OnContactPickedListener. It would have a method like onContactPicked() containing the Uri of the selected contact.

  4. On OnFooEventListener, have something like requestContact() that will be called when the user clicks the button. requestContact() would take an OnContactPickedListener instance as a parameter.

  5. The activity would do the startActivityForResult() call and, in onActivityResult(), call the associated onContactPicked() method on the OnContactPickedListener. The activity would cache those in a HashMap or something while the request was in process.

Now, we have clear separation between the activity and the fragment. The fragment can still be hosted by any number of activities (e.g., one for a large screen, a different one for a normal screen). The contact can be supplied either by the production means (ACTION_PICK) or something else for testing (e.g., value established as part of the test case). The activity can deal with any number of such fragments without issue.

这篇关于替代startActivityForResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆