找到你要的答案

Q:Custom RecyclerAdapter and startActivityForResult

Q:自定义recycleradapter和startActivityForResult

I have a Fragment that contains a RecyclerView which uses a custom RecyclerAdapter. I have an onClickListener inside my custom RecyclerAdapter - when a position is clicked I want it to start startActivityForResult. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment containing the RecyclerView onActivityResult is never called. I have passed in a context to the custom RecyclerAdapter. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult in the MainActivity which has the Fragment container so the Fragment does receive onActivityResult if startActivityForResult is initiated from the Fragment. My code:

RecyclerAdapter onClickListener:

@Override
        public void onClick(View v) {

            String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();

            Intent intent = new Intent(context, CreateItemsActivity.class);
            intent.putExtra("TITLE_ID", titleId);
            ((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
        }

CreateItemsActivity.class - onBackPressed()

    @Override
        public void onBackPressed() {

            Intent intent = new Intent();
            setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
            finish();
        }

MyListsFragment.class (contains RecyclerView)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("CALLED", "OnActivity Result");

        // if requestCode matches from CreateItemsActivity
        if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {

            Log.e("REQUEST CODE", String.valueOf(requestCode));

            populateRecyclerView();
        }
    }

Also I have this in the MainActivity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // included to allow fragment to receive onActivityResult

    }

我有一个片段,包含一个recyclerview使用自定义recycleradapter。我有一个onclicklistener里面我的自定义recycleradapter -当一个位置点击我要开始startActivityForResult。到目前为止,这在工作,如当它点击它开始的活动所需的。然而当我按下按钮去包含recyclerview onActivityResult片段不被。我已经在一个上下文中的自定义recycleradapter通过。这是可能的吗?或是活动/片段启动startActivityForResult是一个拦截吗?如果不是我将结束在一个手势检测器或类似的片段处理的事件,但在那之前我想给这一个公正!注:我已经包括在MainActivity onActivityResult具有片段,片段的容器是否是从片段开始接收startActivityForResult onActivityResult。我的代码:

RecyclerAdapter onClickListener:

@Override
        public void onClick(View v) {

            String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();

            Intent intent = new Intent(context, CreateItemsActivity.class);
            intent.putExtra("TITLE_ID", titleId);
            ((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
        }

createitemsactivity.class - onbackpressed()

    @Override
        public void onBackPressed() {

            Intent intent = new Intent();
            setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
            finish();
        }

MyListsFragment.class(包含recyclerview)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("CALLED", "OnActivity Result");

        // if requestCode matches from CreateItemsActivity
        if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {

            Log.e("REQUEST CODE", String.valueOf(requestCode));

            populateRecyclerView();
        }
    }

我也有这样的主流:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // included to allow fragment to receive onActivityResult

    }
answer1: 回答1:

OK, so IF by any chance somebody else has this problem here is my

solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // get current fragment in container
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.

好吧,如果其他人有这个问题的话

解决方案。我加入这个代码为主要活动的onActivityResult(注意:我有一个框架的容器,是所有片段充气):

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // get current fragment in container
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

我相信这个工作的主要活动是最高的层次和截获onActivityResult,所以基本上我就点它,我希望它是。

answer2: 回答2:

Your calling Activities onActivityResult will only be called when the second activity finishes and a setResult has been executed.

Since the user is hitting the back button the 2nd activity is finishing without setResult being called.

You'll need to override onBackPressed so you can execute your setResult code.

I see you have implemented this but I think the crux of the issue is that you need to return Activity.RESULT_OK not your request code.

@Override
public void onBackPressed() {       
   Intent intent = new Intent();
   setResult(RESULT_OK, intent);

   super.onBackPressed();
}

In this case you don't need to explicitly return your requestCode of Constants.NEW_ITEMS_REQUEST_CODE because Android will forward that automatically.

你的呼叫活动的onActivityResult只会叫当次活动结束和setResult已执行。

由于用户是后退按钮第二活动是整理不被调用setResult。

你需要重写onbackpressed所以你可以执行你的setResult代码。

我看到你已经实现了这个但我认为问题的关键是,你需要返回activity.result_ok不是你的请求代码。

@Override
public void onBackPressed() {       
   Intent intent = new Intent();
   setResult(RESULT_OK, intent);

   super.onBackPressed();
}

在这种情况下,你不需要显式地返回你的constants.new_items_request_code requestCode因为Android将自动。

java  android  android-fragments  recyclerview  onactivityresult