Fragment中的onActivityResult

Fragment中的onActivityResult

??????項目中大量使用了fragment,碎片確實很省內(nèi)存,加載也快,但是相對于activity,開發(fā)中卻會遇到很多的“坑”。項目中有個需求是一個fragment需要從另一個activity中取數(shù)據(jù),然后帶回。首先就想到了onActivityResult,但是真正寫好代碼后,發(fā)現(xiàn)數(shù)據(jù)并沒有返回(接收不到)。正常情況下,onActivityResult是在activity中進行接收,于是在fragment宿主activity中重寫onActivityResult方法,打印日志發(fā)現(xiàn),確實可以接收到。由此,我們可以想到通過在activity的onActivityResult中獲取到數(shù)據(jù)后,將數(shù)據(jù)傳入fragment中即可。
??????抱著這樣的想法,我們可以在fragment中寫好方法并進行處理,在activity中進行調(diào)用并傳入數(shù)據(jù)。

以下為demo代碼:

MainActivity:

package com.kang.fragmentresult;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public FragmentManager fm;
    public static final String MY_TAG = "MYTAG";
    public Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.btn);
        fm = getSupportFragmentManager();
        if (mButton != null) {
            mButton.setOnClickListener(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("data", data.getStringExtra("data"));
        Fragment fragment = fm.findFragmentByTag(MY_TAG);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View view) {
        fm.beginTransaction().add(R.id.fl_content, new GoFragment(), MY_TAG).commit();
    }
}

GoFragment:

package com.kang.fragmentresult;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class GoFragment extends Fragment implements View.OnClickListener {
    public static final int MY_REQUEST_CODE = 2;
    public static final int MY_RESULT_CODE = 1;
    public Button btngo;
    private EditText et;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goview, container, false);
        et = (EditText) view.findViewById(R.id.et);
        btngo = (Button) view.findViewById(R.id.btn_go);
        btngo.setOnClickListener(this);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_REQUEST_CODE) {
            if (resultCode == MY_RESULT_CODE) {
                String str = data.getStringExtra("data");
                et.setText(str);
            }
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), ResultActivity.class);
        getActivity().startActivityForResult(intent, MY_REQUEST_CODE);
    }
}

ResultActivity:

package com.kang.fragmentresult;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class ResultActivity extends AppCompatActivity {
    public static final int MY_RESULT_CODE = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        final TextView textView = (TextView) findViewById(R.id.tv_hello);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("data", textView.getText().toString());
                setResult(MY_RESULT_CODE, intent);
                finish();
            }
        });
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.kang.fragmentresult.MainActivity">

<FrameLayout
    android:id="@+id/fl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="@dimen/activity_horizontal_margin"
            android:text="Jump"
            android:textColor="#ffffff"
            android:textSize="32sp" />


    </LinearLayout>
</FrameLayout>
</LinearLayout>

fragment_goview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">

<Button
    android:id="@+id/btn_go"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_horizontal_margin"
    android:text="go go go"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:hint="@string/app_name"
    android:padding="@dimen/activity_horizontal_margin" />

</LinearLayout>

activity_result:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">

<TextView
    android:id="@+id/tv_hello"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="hello world!"
    android:textSize="66sp" />

</LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容