Fragment中的onActivityResult
项目中大量使用了fragment,碎片确实很省内存,加载也快,但是相对于activity,开发中却会遇到很多的“坑”。项目中有个需求是一个fragment需要从另一个activity中取数据,然后带回。首先就想到了onActivityResult,但是真正写好代码后,发现数据并没有返回(接收不到)。正常情况下,onActivityResult是在activity中进行接收,于是在fragment宿主activity中重写onActivityResult方法,打印日志发现,确实可以接收到。由此,我们可以想到通过在activity的onActivityResult中获取到数据后,将数据传入fragment中即可。
抱着这样的想法,我们可以在fragment中写好方法并进行处理,在activity中进行调用并传入数据。
以下为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>