Home How to save fragment onButtonClick and load on next program start
Reply: 0

How to save fragment onButtonClick and load on next program start

user6399
1#
user6399 Published in May 26, 2018, 9:40 pm

I have an app where I have a fragment.Let's call it mainScreen. In mainScreen there's a button and onClick() opens second fragment. Let's call it FragmentHomePage. In FragmentHomePage i have a retrofit. There's a button named logOut. My problem is, when user not clicked logOut, i want to save this fragment and load this fragment. In default when app starts, opens mainScreen, but if user not clicks logOut, i need open FragmentHomePage on app start. How can i do this?

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();
        }
    });
}}

How can i do this part? Thanks. Yeah and not give negative vote. I'm a beginner on this site)).

Here's the activity where i'm launching the fragments.

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();
}}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307502 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO