一、为什么要写 startActivityForResult()
-
在实际项目中我们会有这样的应用场景,在 Activity1 的 Fragment1 中的某个点击事件,跳转到另一个 Activity2 的 Fragment2 中,而 Fragment2 需要返回一些数据给 Fragment1,这时候我们该怎么办呢
-
通过 Fragment 的 startActivityForResult() 方法可以完成上述的操作,但是这又跟我们平时的 Activity 的 startActivityForResult() 不一样, Fragment 没有 setResult() 方法,无法设置返回的数据。
-
正确的操作思路是:Fragment1 调用 startActivityForResult() 方法之后,跳转到 Activity2 中,Activity2 先把数据取出来,然后通过 Bundle 把数据设置给 Fragment2 ,Fragment2 把数据取出来,然后调用 getActivity().setResult() 方法设置返回数据,然后在 Fragment1 的 onActivityResult() 中拿到返回的数据
二、实际操作
2.1 Fragment1,即 FirstFragment 代码如下:
public class FirstFragment extends Fragment {
@BindView(R.id.listview)
ListView listview;
private String name;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i("FirstFragment", "onCreateView");
View inflate = inflater.inflate(R.layout.huituizhan_first_fragment, container, false);
ButterKnife.bind(this, inflate);
initListViewData();
return inflate;
}
private void initListViewData() {
list = new ArrayList<>();
int m = 0;
for (int i = 0; i < 20; i++) {
list.add("珞神"+m);
m++;
}
myAdapter = new MyAdapter();
listview.setAdapter(myAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击跳转
Intent intent = new Intent(getActivity(),ResultActivity.class);
intent.putExtra("item",position+"");
startActivityForResult(intent,0);
}
});
}
/**
* 调用 startActivityForResult 后,目标activity 返回的数据会在这个方法内
* @param requestCode
* @param resultCode
* @param data
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
list.add(requestCode+resultCode+data.getStringExtra("item"));
myAdapter.notifyDataSetChanged();
}
private ArrayList<String> list;
private MyAdapter myAdapter;
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder viewHolder;
if (convertView == null) {
view = View.inflate(getActivity(), R.layout.litt_item, null);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.listText.setText(list.get(position));
return view;
}
}
static class ViewHolder {
@BindView(R.id.list_text)
TextView listText;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
2.2 Activity2 即 ResultActivity 的代码如下:
public class ResultActivity extends AppCompatActivity {
@BindView(R.id.framelayout_resultfragment)
FrameLayout framelayoutResultfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sencond);
ButterKnife.bind(this);
//取出传递过来的数据, 注意 newInstance 方法设置的有 Bundle
String title = getIntent().getStringExtra("item");
ResultFragment resultFragment = ResultFragment.newInstance(title);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.framelayout_resultfragment,resultFragment,"resultfragment")
.commit();
}
}
注意:ResultFragment 一定要是同一个对象,不要创建两个对象
2.3 Fragment2 即 ResultFragment 的代码如下:
package com.example.administrator.fragment_text.huituizhan;
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 com.example.administrator.fragment_text.R;
public class ResultFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
String item = bundle.getString("item");
Log.i("ResultFragment",item+"");
Intent intent = new Intent();
intent.putExtra("item","result");
getActivity().setResult(1,intent);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_result, container, false);
}
//返回 resultFragment 对象,并把传递过来的数据设置在 bundle 中
public static ResultFragment newInstance(String argument){
Bundle bundle = new Bundle();
bundle.putString("item", argument);
ResultFragment resultFragment = new ResultFragment();
resultFragment.setArguments(bundle);
return resultFragment;
}
}
注意: 这里的逻辑是,在创建 ResultFragment 对象的时候,就把数据设置进 Bundle 中了,然后在 onCreate 方法中取出 bundle 中的数据,并设置返回的数据。
网友评论