Android JetPack-Navigation使用

一,简介:navigation简化(管理)Fragment操作


// https://mvnrepository.com/artifact/androidx.navigation/navigation-fragment
    implementation group: 'androidx.navigation', name: 'navigation-fragment', version: '2.0.0'
// https://mvnrepository.com/artifact/androidx.navigation/navigation-ui
    implementation group: 'androidx.navigation', name: 'navigation-ui', version: '2.0.0'

先看看图:
Android JetPack-Navigation使用_第1张图片

直接看步骤:
第一步新建XML文件
Android JetPack-Navigation使用_第2张图片
内容:



    
        
        
    
    
        
    
    
        
    

Xml直接拖拽 里面的代码自动生成,需要记住action的id即可,后面会用
Android JetPack-Navigation使用_第3张图片
MainActivity.

public class MainActivity extends AppCompatActivity {
    private DataViewModel model;
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        model = ViewModelProviders.of(this).get(DataViewModel.class);
    }
    @Override
    public boolean onSupportNavigateUp() {
        return true;

    }
}

Xml

  
        


    

FragmentThere

public class ThereFragment extends Fragment {
    FragmentThereBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_there, container, false);
        View view = binding.getRoot();
        binding.anniu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //穿值过去
                Bundle bundle = new Bundle();
                bundle.putString("one", "这是一条很大的船");
                Navigation.findNavController(view).navigate(R.id.action_thereFragment2_to_oneFragment2, bundle);
            }
        });
        binding.anniu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //穿值过去
                Bundle bundle = new Bundle();
                bundle.putString("two", "这是大海!");
                Navigation.findNavController(view).navigate(R.id.action_thereFragment2_to_twoFragment2,bundle);
            }
        });
        binding.anniu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //	返回
                Navigation.findNavController(view).navigateUp();
            }
        });

        return view;

    }


}

FragmentOne

public class OneFragment extends Fragment {
    FragmentOneBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_one, container, false);
        Bundle bundle=getArguments();
        String data=bundle.getString("one");
        binding.tvname.setText(data);
        binding.fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   //第一種返回
//                Navigation.findNavController(view).navigate(R.id.action_oneFragment2_to_thereFragment2);
                //第二種返回
//                Navigation.findNavController(getView()).popBackStack();
                //第三種返回
                Navigation.findNavController(getView()).navigateUp();
                //popBackStack() 如果当前的返回栈是空的就会报错,因为栈是空的了,navigateUp() 则不会,还是停留在当前界面
            }
        });
        return binding.getRoot();
    }


}

至于XML 自己随便写。就不贴了
重点代码
1,单纯跳转

id:目的的ID,也就是Action动作的ID。
 Navigation.findNavController(view).navigate(R.id.action_thereFragment2_to_oneFragment2);
 2,带数据跳转
    Bundle bundle = new Bundle();
                bundle.putString("two", "这是大海!");
                Navigation.findNavController(view).navigate(R.id.action_thereFragment2_to_twoFragment2,bundle);
              

3.接收

      Bundle bundle=getArguments();
        String data=bundle.getString("one");

4,返回

     //第一種返回
//                Navigation.findNavController(view).navigate(R.id.action_oneFragment2_to_thereFragment2);
                //第二種返回
//                Navigation.findNavController(getView()).popBackStack();
                //第三種返回
                Navigation.findNavController(getView()).navigateUp();
                //popBackStack() 如果当前的返回栈是空的就会报错,因为栈是空的了,navigateUp() 则不会,还是停留在当前界面

你可能感兴趣的:(Android)