From Activity to Fragmet with shared Preferences

  android, android-studio, performance

I try to go from DataHelpActivity to DateFragment. I try with manny different ways but nothing much on my code. Take a look of my code.
DateHelpActivity.class

package com.example.emvolioapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DateHelpActivity extends AppCompatActivity {

    TextView msname,mfname,mamka,mphone,memail;
    Button mcancel;
    SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_help);

        msname = findViewById(R.id.sname);
        mfname = findViewById(R.id.fname);
        mamka = findViewById(R.id.amka);
        mphone = findViewById(R.id.phone);
        memail = findViewById(R.id.email);
        mcancel = findViewById(R.id.cancel);

        sharedPreferences = getSharedPreferences("SHARED_PREF", MODE_PRIVATE);
        String fname = sharedPreferences.getString("NAME","");
        mfname.setText(fname);
        String sname = sharedPreferences.getString("SURNAME","");
        msname.setText(sname);
        String amka = sharedPreferences.getString("AMKA","");
        mamka.setText(amka);
        String phone = sharedPreferences.getString("PHONE","");
        mphone.setText(phone);
        String mail = sharedPreferences.getString("MAIL","");
        memail.setText(mail);

        mcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.apply();


                Intent intent = new Intent(DateHelpActivity.this,DateFragment.class);
                startActivity(intent);
                finish();
            }
        });

    }
}

Then i want to go to this class which is actually a fragment from a costume navigation drawer.
Take a look:

package com.example.emvolioapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class DateFragment extends Fragment {
    //variables for input
    TextInputLayout fname,sname,amka,phone,email;
    CheckBox mRemem;
    Button send, cancel;
    FirebaseDatabase rootNode;
    DatabaseReference reference;
    SharedPreferences sharedPreferences;
    boolean isRemem = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_date,container,false);
        fname = view.findViewById(R.id.fname);
        sname = view.findViewById(R.id.sname);
        amka = view.findViewById(R.id.amka);
        phone = view.findViewById(R.id.phone);
        email = view.findViewById(R.id.email);
        send = view.findViewById(R.id.butReq);
        mRemem = view.findViewById(R.id.checkbox);

        sharedPreferences = this.getActivity().getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE);

        isRemem = sharedPreferences.getBoolean("CHECKBOX",false);
        if(isRemem){
            Intent intent = new Intent(getActivity(),DateHelpActivity.class);
            startActivity(intent);
            getActivity().finish();
        }

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rootNode = FirebaseDatabase.getInstance();
                reference = rootNode.getReference("Requests");
                String name = fname.getEditText().getText().toString();
                String surname = sname.getEditText().getText().toString();
                String myamka = amka.getEditText().getText().toString();
                String phonenum = phone.getEditText().getText().toString();
                String mymail = email.getEditText().getText().toString();
                boolean checked = mRemem.isChecked();

                HelperClassDatabase helperClassDatabase = new HelperClassDatabase(name,surname,myamka,phonenum,mymail);
                reference.child(name).setValue(helperClassDatabase);

                //save data
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("NAME",name);
                editor.putString("SURNAME",surname);
                editor.putString("AMKA",myamka);
                editor.putString("PHONE",phonenum);
                editor.putString("MAIL",mymail);
                editor.putBoolean("CHECKBOX",checked);
                editor.apply();

                Toast.makeText(getActivity(), "Infrormation saved", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getActivity(),DateHelpActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        return view;
    }
}

Have any ideas?
I believe that the mistake is on this part of code.

mcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.apply();


                Intent intent = new Intent(DateHelpActivity.this,DateFragment.class);
                startActivity(intent);
                finish();
            }
        });

Source: Android Questions

LEAVE A COMMENT