A
A
Andrey Goroshko2018-08-16 12:01:23
Android
Andrey Goroshko, 2018-08-16 12:01:23

How to call a fragment method from another fragment?

I am working with an application in which there are two lists of messages, these lists are placed in fragments, the data for filling the lists is taken from the server, in order for the server to give us this data, we need an access_token, which eventually goes bad and you need to send a request for a new one, the response comes a new token that I save in SharedPreferences. So, actually, the question itself is how, after receiving a new token, call the fragment I need and recreate it. Here is the method I use to update tokens:

mAPIService2.getNewToken(new ReqAccessToken(token)).enqueue(new Callback<ResNewTokens>() {
            @Override
            public void onResponse(@NonNull Call<ResNewTokens> call, @NonNull Response<ResNewTokens> response) {
                if (response.isSuccessful()) {

                    String n_access_token = Objects.requireNonNull(response.body()).getAccess_token();
                    String n_refresh_token = Objects.requireNonNull(response.body()).getRefresh_token();

                    new_acc_tok = getActivity().getSharedPreferences("access_token", 0);
                    SharedPreferences.Editor editor = new_acc_tok.edit();
                    editor.putString(ACCESS_TOKEN, n_access_token);
                    editor.apply();

                    new_ref_tok = getActivity().getSharedPreferences("refresh_token", 0);
                    SharedPreferences.Editor editor1 = new_ref_tok.edit();
                    editor1.putString(REFRESH_TOKEN, n_refresh_token);
                    editor1.apply();

                    received();

                } else {
                    ResponseBody errorBody = response.errorBody();
                    try {
                        if (Objects.requireNonNull(errorBody).string().contains("refresh_token_expired")) {
                            logOut();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResNewTokens> call, @NonNull Throwable t) {

            }

as you can see from the code, after receiving the tokens, I call a method that pulls a list of messages from the server, taking into account the new access_token, and when I open the tab with sent messages, I have an empty list, and a message is displayed in the logs that the access_token is rotten and needs to be updated, in short, in As a result, I am thrown out to the login screen, after the message that the refresh is no longer working. I will not be able to find a normal way that recreates a fragment, or rather, I need not the entire fragment can be recreated, but only call a function that is in another fragment. After all, if you call a fragment, then only to replace the current one. In short, the situation is not very clear, to be honest. If anyone knows how to fix the current situation, then I will be glad to help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question