(8)调用Fragment自身的startActivityForResult并在Fragment中接收返回结果

步骤归纳

  • Fragment跳转时直接调用startActivityForResult()方法,前面无需添加getActivity()
 Intent intent = new Intent(getActivity(), ContentActivity.class);
 startActivityForResult(intent, REQUEST_CODE);
  • Activity中设置返回标志并传参
  Intent intent = new Intent(ContentActivity.this, MainActivity.class);
  intent.putExtra("test", 111);
  setResult(RESULT_OK, intent);
  finish();
  • Fragment中处理返回结果
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                return;
            }
            if (data != null) {
                /**
                 * 取参
                 * */
                int param = data.getIntExtra("test", 0);
                Log.i(TAG, "onActivityResult: param:" + param);
            }
            Log.i(TAG, "onActivityResult: resultCode" + resultCode + ",requestCode:" + requestCode);
            //do something
        }
    }

一个小例子

流程:MainActivity包含一个ContentFragment,点击ContentFragment中的按钮,调用startActivityForResult方法跳转到ContentActivity,然后点击ContentActivity中的返回按钮,执行setResult方法

代码:
MainActivity
package com.xzy.fragmentbasicdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.xzy.fragmentbasicdemo.fragment.ContentFragment;

/**
 * 测试fragment的一些用法:
 * 1.startActivityForResult用法
 */
public class MainActivity extends FragmentActivity {

    private ContentFragment mContentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        /**
         * 在activity_main的根布局添加ID属性,值为id_fragment_container
         * */
        mContentFragment = (ContentFragment) fm.findFragmentById(R.id.id_fragment_container);
        if (mContentFragment == null) {
            mContentFragment = new ContentFragment();
            fm.beginTransaction().replace(R.id.id_fragment_container, mContentFragment).commit();
        }

    }
}

ContentFragment
package com.xzy.fragmentbasicdemo.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.xzy.fragmentbasicdemo.ContentActivity;
import com.xzy.fragmentbasicdemo.R;

import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContentFragment extends Fragment {
    private static final String TAG = "ContentFragment";

    private static final int REQUEST_CODE = 0x11;


    public ContentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        Button button = view.findViewById(R.id.btn_test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ContentActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                return;
            }
            if (data != null) {
                /**
                 * 取参
                 * */
                int param = data.getIntExtra("test", 0);
                Log.i(TAG, "onActivityResult: param:" + param);
            }
            Log.i(TAG, "onActivityResult: resultCode" + resultCode + ",requestCode:" + requestCode);
            //do something
        }
    }
}
ContentActivity
package com.xzy.fragmentbasicdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class ContentActivity extends AppCompatActivity {
    private static final String TAG = "ContentActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        Button button = findViewById(R.id.btn_back);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ContentActivity.this, MainActivity.class);
                intent.putExtra("test", 111);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

}

参考文章


  • http://blog.csdn.net/noobzsb/article/details/70312365#comments





你可能感兴趣的:(Android,四大组件)