java—如何将包从一个活动传递到另一个活动中的片段?

hjzp0vay  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(320)

我曾用包传递旗帜,但它没有工作!

PersonalInfoFragment fragment=new Personal Info Fragment();
 Bundle bundle=new Bundle();
 bundle.put String("key","update");
 fragment.setArguments(bundle);
 Intent intent=new Intent(DataDisplayActivity.this,ProfileCompletionActivity.class);
 startActivity(intent);
y53ybaqx

y53ybaqx1#

您需要在片段管理器的帮助下加载片段。然后你就可以在片段中传递数据了。
主要活动.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

            // create fragment instance
            val fragment : FragmentName = FragmentName.newInstance()

            // for passing data to fragment
            val bundle = Bundle()
            bundle.putString("data_to_be_passed", DATA)
            fragment.arguments = bundle

            // check is important to prevent activity from attaching the fragment if already its attached
            if (savedInstanceState == null) {
                supportFragmentManager
                    .beginTransaction()
                    .add(R.id.fragment_container, fragment, "fragment_name")
                    .commit()
            }
        }

    }
}

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

碎片名称.kt

class FragmentName : Fragment() {

        companion object {
            fun newInstance() = FragmentName()
        }

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

            // receiving the data passed from activity here
            val data = arguments!!.getString("data_to_be_passed")
            return view
        }

        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
        }

    }
a11xaf1n

a11xaf1n2#

从第一个活动到第二个活动的第一次传递值,如

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
        intent.putExtra("value",value.toString);
        startActivity(intent);

在第二个活动中获得价值,比如

Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            value = bundle.getString("value","");
        }

当第二个活动加载时,将其值发送到片段,如

FragmentManager fragmentManager = getSupportFragmentManager();
                            Bundle bundle = new Bundle();
                            bundle.putString("value", "anyvalue you want to send");
                            PersonalInfoFragment frag = new PersonalInfoFragment();
                            frag.setArguments(bundle);
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            FragmentTransaction fragmentTransaction.replace(R.id.fragment_container, frag, null);
                            fragmentTransaction.addToBackStack(null);
                            fragmentTransaction.commit();

现在在片段中获取值,比如

if(getArguments() != null) {
            totalValue = getArguments().getString("value");
        }

相关问题