Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

android - how to pass data from one fragment to previous fragment?

I am going FragmentA->FragmentB. Now From FragmentB I want to pass data to FragmentA.So How can I do that??

Currently am going FragmentB->FragmentA with getCustomFragmentManager().popBackStack(); but not passing any value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In FragmentA

    FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
    FragmentB frgamentB = new FrgamentB();
    fragmentB.setTargetFragment(FrgamentA.this, AppConstant.FRAGMENT_CODE);
    ft.addToBackStack(fragment.getClass().getName());
    ft.add(R.id.content, fragment, tag);
    ft.commit();

Now Override onActivityResult() in FragmentA

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
      if (requestCode==AppConstant.FRAGMENT_CODE){
         int addID = data.getIntExtra("addressID", 0);
         String addressLine=data.getStringExtra("addressLine");
      }
    }
}

In FragmentB (on Click of back Button)

Intent intent = new Intent(context, FragmentB.class);
intent.putExtra("addressID", list.get(position).getId());
intent.putExtra("addressLine", list.get(position).getAddressLine1());
getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK, intent);
getFragmentManager().popBackStack();

Update In Kotlin

FragmentExtention

fun Fragment.addFragment(fragment: Fragment,targetFragment: Fragment,requestCode: Int, isAddToBackStack: Boolean = false, init: Bundle.() -> Unit = {}) {
    requireFragmentManager().inTransaction {
        val bundle = Bundle()
        bundle.init()
        fragment.arguments = bundle
        setTargetFragment(targetFragment,requestCode)
        if (isAddToBackStack) {
            addToBackStack(fragment::class.java.simpleName)
        }
        add(R.id.content, fragment, fragment::class.java.simpleName)
    }
}


inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
        val fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }

fun Fragment.removeFragment(fragment: Fragment, enter: Int = 0, exit: Int = 0) {
    requireFragmentManager().inTransaction {
        remove(fragment)
    }
}

In FragmentA

addFragment(FrgamentB(),this,AppConstant.FRAGMENT_CODE,true)

Now Override onActivityResult() in FragmentA

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
            val addID = data?.getIntExtra("addressID", 0)
            val addressLine = data?.getStringExtra("addressLine")
        }
 }

In FragmentB (on Click of back Button)

val intent = Intent(context, FragmentB::class.java)
intent.putExtra("addressID", list.get(position).getId()) 
intent.putExtra("addressLine",list.get(position).getAddressLine1())                
targetFragment.onActivityResult(targetRequestCode,RESULT_OK, intent)
fragmentManager.popBackStack()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...