【问题标题】:return value to fragment from activity android从活动android返回值到片段
【发布时间】:2014-12-30 09:51:15
【问题描述】:

我有调用活动(A)的片段(F)。在活动(A)中按下按钮(B)时,活动必须将选定的数组列表值返回给片段(F)并完成活动(A)......这可能吗?

我从 Activity 知道,您可以按以下方式发送数据:

Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
Fragmentclass fragmentobj = new Fragmentclass();
fragmentobj.setArguments(bundle);

在 Fragment 的 onCreateView 方法中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}

但这适用于调用不使用相同片段的新片段。

我的问题:被调用的 Activity 是否可以将值返回给调用者片段?

public class HomeFragment extends Fragment{
    Button attachment;
    String strtext = "check";   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
    }   

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.home_fragment, null);     
        attachment = (Button) rootView
                .findViewById(R.id.btn_activity);       
        Toast.makeText(getActivity(),
                "Key= " + strtext,
                Toast.LENGTH_LONG).show();
        attachment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent(getActivity(), AndroidCustomGalleryActivity.class);
                getActivity().startActivityForResult(myIntent,999); 

            }
        });

        return rootView;

    }   

    public void onActivityResult(int requestCode, int resultCode, Intent data) {        

        if(requestCode == 999){
        strtext = getArguments().getString("key"); 

        Toast.makeText(getActivity(),
                "Key= " + strtext,
                Toast.LENGTH_LONG).show();
        }
    }
}

在被调用的活动中

Bundle bundle = new Bundle();
bundle.putString("key", "value");
HomeFragment fragmentobj = new HomeFragment();
fragmentobj.setArguments(bundle);
finish();

【问题讨论】:

    标签: android-activity android-fragments


    【解决方案1】:

    在你的fragment(F)中调用startActivityForResult()方法来启动activity A,然后重写fragment中的onActivityResult()方法。

    所以当活动 A 完成时,您可以在 onActivityResult() 方法中获取结果包。

    编辑

    在您的片段 onClick() 中像这样调用活动

    Intent myIintent=new Intent(getActivity(), AndroidCustomGalleryActivity.class);
        startActivityForResult(myIintent, 999);
    

    在你的活动中

    Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putString("key", "value");
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    

    现在您将在片段的 onActivityResult() 方法中获得结果。

    EDIT2

    在您的片段 onActivityResult() 方法中,您必须像这样获取数据...

    Bundle extras = data.getExtras();
        strtext =   extras.getString("key");
    

    【讨论】:

    • 我按照你告诉我的做了,并在上面发布了代码你能告诉这是你的意思,因为它仍然无法工作......
    • 如果 getActivity() 从 startActivityForResult(myIintent, 999) 中删除;然后应用程序崩溃......如果不删除工作正常但没有结果
    • 你遇到了什么异常?
    • java.lang.RuntimeException: 传递结果失败 ResultInfo{who=android:fragment:0, request=999, result=-1, data=Intent { cmp=com.isummation.customgallery/.AndroidCustomGalleryActivity (有附加功能)}} 到活动 {com.isummation.customgallery/com.isummation.customgallery.MainActivity}:java.lang.NullPointerException
    • 查看我编辑的答案2,并检查您的活动代码...必须在给定的活动中编写代码??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多