Message passing between Activities in Android

Project scenario:

        Recently, I am writing a cloud platform data acquisition project. When I log in, I need to always carry the cookie value returned after successful login to ensure that the subsequent interface access is unimpeded. This requires storing the cookie value between different activities. Pass or pass to Fragment


Problem Description

        When transferring between activities, the method of transferring data through the interface is used, but when it is in the Fragment, once it enters the thread, it will report null and a null pointer exception; the initial idea is to use the method of static code blocks. Empty judgment operation (*but my Fragment uses ViewPager+Fragment+BottomNavgationView to realize the bottom navigation bar), so I use polymorphic method to modify, emmmmm still cannot be empty or empty! !


solution:

Here I use Intent to transfer data, SharedPreferences lightweight storage method for data transfer

1. Intent transfer data

        Data is passed through the Intent's putExtra() method, and another activity can receive data through the getExtra() method.

Note: here is  to pass data from SecondFragment  to  ActivitySensorPage 

(1) In SecondFragment

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MessageBean messageBean = messageBeanList.get(position);
                Toast.makeText(getActivity(), messageBean.getName(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), ActivitySensorPage.class);
                intent.putExtra("uid", messageBean.getUid());
                intent.putExtra("token", messageBean.getToken());
                Toast.makeText(getActivity(), Data_uid + Data_token, Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });

(2) In ActivitySensorPage

        String Data_uid  = null,Data_token  = null;
        Intent intent = getIntent();
        Data_uid = intent.getStringExtra("uid");
        Data_token = intent.getStringExtra("token");
        System.out.println("ActivitySensorPage中接收到的数据 = " + Data_uid + " 和 " + Data_token);
      

2. SharedPreferences transfer data

        SharedPreferences is a lightweight storage method in Android, which can be used to store application configuration files and some simple data.

Note: Here is ActivityLoginPage passing data to SecondFragment

(1) in ActivityLoginPage

SharedPreferences.Editor editor = ActivityLoginPage.this.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit();
                                    editor.putString("DATA", GetCookie);
                                    editor.apply();

(2) In SecondFragment

SharedPreferences sharedPreferences = getActivity().getSharedPreferences("DATA", MODE_PRIVATE);
        data = sharedPreferences.getString("DATA", "");

Note: When using Fragment to get data in Activity, there will be a reminder that it may be a null pointer! ! ! We need to get the data in the Activity in the onAttach() method and assign it to a member variable, and then use the member variable where we need to use getActivity(); here is the reference link https://www.cnblogs.com/tingtasia/p/ 11545014.html

Guess you like

Origin blog.csdn.net/weixin_47024334/article/details/130861371