Custom RecyclerAdapter and startActivityForResult

走远了吗. 提交于 2019-12-06 11:18:17

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!