前面看完了lifecycle,下面看navigation。 步骤1:引入库
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
步骤2:res下新建navigation目录,再创建个nav_graph的xml 步骤3:创建fragment容器
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost= "true"
app:navGraph="@navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
步骤4:编写nav_graph.xml,或者进行界面拖动操作,其中还可以进行选中进行添加参数
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_gra"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.example.myjetpacktest.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" >
<action
android:id="@+id/action_blankFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.myjetpacktest.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<argument
android:name="age"
app:argType="integer"
android:defaultValue="0" />
<argument
android:name="name"
app:argType="string"
android:defaultValue="li" />
</fragment>
</navigation>
步骤5:参数常量
public class ConstantUtil {
public static final String PARAMS_ONE = "params1";
public static final String PARAMS_TWO = "params2";
}
步骤6:fragment携带参数跳转
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container, false);
view.findViewById(R.id.tv_to_second_fragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putInt(PARAMS_ONE,10);
bundle.putString(PARAMS_TWO,"liwenpeng");
Navigation.findNavController(view).navigate(R.id.action_blankFragment_to_secondFragment,bundle);
}
});
return view;
}
SecondFragment去接收:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getInt(ConstantUtil.PARAMS_ONE);
mParam2 = getArguments().getString(ConstantUtil.PARAMS_TWO);
Log.d(TAG,"onCreate mParam1:"+mParam1+" ;mParam2:"+mParam2);
}
}
|