我有一个应用程序,其中有一个片段。我们称它为 mainScreen。在 mainScreen 中有一个button
并onClick()
打开第二个片段。我们称它为 FragmentHomePage。在 FragmentHomePage 我有一个retrofit
. 有一个名为 logOut 的按钮。我的问题是,当用户没有点击注销时,我想保存这个片段并加载这个片段。默认情况下,当应用程序启动时,会打开 mainScreen,但如果用户没有点击 logOut,我需要在应用程序启动时打开 FragmentHomePage。我怎样才能做到这一点?
public class FragmentHomePage extends BaseFragment {
View mainView;
TextView fullName, userName, email;
Button logOut;
ApiClient apiClient = ApiClient.getInstance();
SupportObjToken supportopToken = new SupportObjToken();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.home_page, container, false);
init(mainView);
newTokenCall();
return mainView;
}
private void init(View v) {
fullName = v.findViewById(R.id.fullName);
userName = v.findViewById(R.id.user);
email = v.findViewById(R.id.mail);
logOut = v.findViewById(R.id.logOut);
}
public void newTokenCall() {
String clientID = SharedPreferencesManager.getInstance().getClientID();
String clientSecret = SharedPreferencesManager.getInstance().getClientSecret();
String refreshToken = SharedPreferencesManager.getInstance().getRefreshToken();
String newRefreshToken = SharedPreferencesManager.getInstance().getNewRefreshToken();
final String firstName = SharedPreferencesManager.getInstance().getFirstName();
final String lastName = SharedPreferencesManager.getInstance().getLastName();
final String mail = SharedPreferencesManager.getInstance().getEmail();
final String user = SharedPreferencesManager.getInstance().getUsername();
supportopToken.setGrantType("refresh_token");
supportopToken.setClientId(clientID);
supportopToken.setClientSecret(clientSecret);
supportopToken.setRefreshToken(refreshToken);
Call<ResponseBody> newToken = apiClient.newToken(supportopToken);
newToken.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String newDataAccess = response.body().string();
JSONObject obj = new JSONObject(newDataAccess);
String newAccessToken = obj.getString("accessToken");
String newRefreshToken = obj.getString("refreshToken");
SharedPreferencesManager.getInstance().setNewAccessToken(newAccessToken);
SharedPreferencesManager.getInstance().setNewRefreshToken(newRefreshToken);
fullName.setText(firstName + " " + lastName);
userName.setText(user);
email.setText(mail);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
} else if (response.code() == 401) {
supportopToken.setRefreshToken(SharedPreferencesManager.getInstance().getNewRefreshToken());
Call<ResponseBody> newToken1 = apiClient.newToken(supportopToken);
newToken1.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String newDataAccess = response.body().string();
JSONObject obj = new JSONObject(newDataAccess);
String newAccessToken = obj.getString("accessToken");
String newRefreshToken = obj.getString("refreshToken");
SharedPreferencesManager.getInstance().setNewAccessToken(newAccessToken);
SharedPreferencesManager.getInstance().setNewRefreshToken(newRefreshToken);
fullName.setText(firstName + " " + lastName);
userName.setText(user);
email.setText(mail);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "You're on failure getting new Token", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "You're on failure getting new Token", Toast.LENGTH_SHORT).show();
}
});
}}
我该怎么做这部分?谢谢。是的,不投反对票。我是这个网站的初学者))。
这是我启动片段的活动。
public class MainActivity extends AppCompatActivity implements FragmentChangeListener {
FragmentActivity fragmentActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiClient.initializeInstance("simple web page");
fragmentActivity = new FragmentActivity();
this.replaceFragment(fragmentActivity, true);
SharedPreferencesManager.init(this);
}
@Override
public void replaceFragment(BaseFragment fragment, Boolean isAddToBackStack) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment, fragment.toString());
transaction.addToBackStack(backStateName);
transaction.commit();
}}