Answer the question
In order to leave comments, you need to log in
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) {
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question