Fragment and Activity data transfer problem

Write in front

In the recent project, the function of the circle of friends encountered, the module of the circle of friends is nested in the Fragment. When publishing an activity, it jumps to another Activity and returns to this Fragment after publishing. After returning, I did not request the background to refresh, but returned the dynamic related information released by the user to the Fragment through the Intent.

Related documents

1. Activity that contains Fragment, InformixActivity
2. Post the Activity that fills in the dynamic information, AddDynamicActivity
3. Fragment, ForumFragment where the circle of friends is located

Business Process

The user clicks the publish button on the ActionBar in the Fragment of Moments.
Jump to the dynamic filling page, AddDynamicActivity . After filling in the page, click Publish or Cancel to return to the Moments of Friends Fragment.
When returning the Fragment, the data must be passed through InformixActivity before it can be successfully passed.

Main code

  1. In InformixActivity , click the publish button
(findViewById(R.id.homeTV)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(InformixActivity.this, AddDynamicActivity.class);
                        // 将要跳转的发布动态Activity会携带数据返回当前Activity
                        startActivityForResult(intent, 1);
                    }
                });

Jump here through the startActivityForResult method. The second parameter is requestCode, in order to determine the return result. This method can carry data back to the Activity before the jump after jumping to the new Activity and after the new Activity is closed.
It should be noted that to get the returned data, the onActivityResult method must be rewritten in the original Activity, that is, InformixActivity , which will be described later.

  1. In AddDynamicActivity , rewrite the left and right button methods of ActionBar to cancel and share respectively.
private void actionBarOnClick(){
        // 重写取消点击事件
        cancelBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 回调onActivityResult
                Intent intent = new Intent();
                Bundle dynamic = new Bundle();
                dynamic.putString("CONTENT", "");
                intent.putExtra("DYNAMIC",dynamic);
                setResult(1, intent);
                AddDynamicActivity.this.finish();
            }
        });
        // 重写分享点击事件
        shareBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 携带数据返回上个Activity栈
                Intent intent = new Intent();
                Bundle dynamic = new Bundle();
                // 判断发布分享的内容是否为空
                if (dynamicContent.getText().toString().trim().length() > 0) {
                    dynamic.putString("CONTENT", dynamicContent.getText().toString().trim());
                    dynamic.putString("TIME", getFormatSysDate("yyyy-MM-dd HH:mm:ss"));
                    intent.putExtra("DYNAMIC", dynamic);
                }else {
                    // 如果发布内容为空,点击分享和点击取消一样
                    dynamic.putString("CONTENT", "");
                    intent.putExtra("DYNAMIC",dynamic);
                }
                setResult(1, intent);
                AddDynamicActivity.this.finish();
            }
        });
    }

As you can see in the code above, there is a **setResult()** method. The first parameter is requestCode, which is used to mark that the returned result is to be returned to the Activity with the same requestCode.
In the above code, I wrote the method of publishing and canceling. The result of publishing empty dynamic and canceling is the same. It is judged by the value of "CONTENT" of the Bundle that returns the data.

  1. Bring the result back to the InformixActivity page, and override the onActivityResult method.
/**
     * 获取AddDynamicActivity返回的数据
     *
     * Ps:先返回到Activity,然后再从当前Activity传递数据到需要的Fragment
     *
     * @param requestCode 请求码
     * @param resultCode 返回码
     * @param data 数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 跳转到朋友圈的onActivityResult
        forumFrag.onActivityResult(requestCode, resultCode, data);
    }

In the onActivityResult method, call the onActivityResult method of ForumFragment to get the data carried in the Fragment.

4. Override the onActivityResult method in ForumFragment .

/**
     * 获取返回的数据,在Fragment向Activity请求,需要先返回到父Activity,然后传递到Fragment
     * @param requestCode 请求码
     * @param resultCode 返回码
     * @param data 数据
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            // 判断发布内容是否为空,是否是取消按钮
            if (!"".equals(data.getBundleExtra("DYNAMIC").getString("CONTENT"))){
                Map<String, String> addDynamic = new HashMap<>();
                addDynamic.put("CREATEUSER", GlobalField.getUser().getName());
                addDynamic.put("XWNR", data.getBundleExtra("DYNAMIC").getString("CONTENT"));
                addDynamic.put("TPID", "");
                addDynamic.put("CREATEDATE", data.getBundleExtra("DYNAMIC").getString("TIME"));
                // 把发布的动态插入数据库
                setDynamicData(addDynamic);
            }
        }
    }

In the above, through requestCode to determine that it is the data we need, we can perform the operations we need.

Write at the end

The process of data transfer between a Fragment of an Activity and another Activity is probably like this.

I’m Xiaobai, what’s wrong, please correct me.

Guess you like

Origin blog.csdn.net/A_Intelligence/article/details/85005990