Pass onActivityResult() data to the same Fragment which is not yet ready

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:31:57

问题


I am using a Fragment to start a new Activity using startActivityForResult(), I am getting the result (Bundle) in onActivityResult() method.Since onActivityResult() called before onResume().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action.

What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.


回答1:


Try this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            mResultBundle = data.getExtras(); //mResultBundle is in fragment 
            //scope
        }
    }
}

@Override
protected void onResume(){
    super.onResume();
    if(mResultBundle != null){
       // process saved bundle from activity result here

       // don't forget to set it back to null once you are done
       mResultBundle = null;
    }
}


来源:https://stackoverflow.com/questions/35476960/pass-onactivityresult-data-to-the-same-fragment-which-is-not-yet-ready

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