【问题标题】:how to pass bundle from an activity to a fragment which is placed in another activity?如何将捆绑从一个活动传递到放置在另一个活动中的片段?
【发布时间】:2020-05-28 20:03:14
【问题描述】:
我用过bundle来传递flag,但是没用!
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);
【问题讨论】:
标签:
java
android
android-fragments
android-intent
【解决方案1】:
您需要在片段管理器的帮助下加载片段。比你可以传递片段中的数据。
MainActivity.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()
}
}
}
}
activity_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)
}
}
【解决方案2】:
从第一个活动到第二个活动的第一个传递值,例如
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");
}