Search code examples
androidandroid-viewpagerfragmentandroid-lifecyclefragment-lifecycle

how to refresh the edittext after the alertdialog is closed


I want to change the values of the 3 edittexts after the user presses confirm in the alertdialog.But it is not refreshing.the values are only changing after moving to some other fragment and coming back to this fragment

SharedPreferences sp;
EditText ip,port,appkey;
Button connect;

public ConnectFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.fragment_connect, container, false);
    ip= (EditText) v.findViewById(R.id.editid);
    port= (EditText) v.findViewById(R.id.editport);
    appkey= (EditText) v.findViewById(R.id.editsppKey);
    Toast.makeText(getActivity(),"oncreateview",Toast.LENGTH_SHORT).show();
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
    ip.setText(sp.getString("ip",null));
    port.setText(sp.getString("port",null));
    appkey.setText(sp.getString("appkey",null));
    connect= (Button) v.findViewById(R.id.connectf);
   connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            senddata();
        }
    });
    return v;
}
private void senddata() {
    //senddata

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Toast.makeText(getActivity(),"on Activity Created",Toast.LENGTH_SHORT).show();
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
    ip.setText(sp.getString("ip",null));
    port.setText(sp.getString("port",null));
    appkey.setText(sp.getString("appkey",null));
}


@Override
public void onAttachFragment(Fragment childFragment) {
    super.onAttachFragment(childFragment);
    Toast.makeText(getActivity(),"On Attach Fragment",Toast.LENGTH_SHORT).show();
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
    ip.setText(sp.getString("ip",null));
    port.setText(sp.getString("port",null));
    appkey.setText(sp.getString("appkey",null));
}

@Override
public void onResume() {
    super.onResume();
    Toast.makeText(getActivity(),"On Resume",Toast.LENGTH_SHORT).show();
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
    ip.setText(sp.getString("ip",null));
    port.setText(sp.getString("port",null));
    appkey.setText(sp.getString("appkey",null));
}

@Override
public void onStop() {
    super.onStop();
    Toast.makeText(getActivity(),"stop",Toast.LENGTH_SHORT).show();
}

@Override
public void onPause() {
    super.onPause();
    Toast.makeText(getActivity(),"pause",Toast.LENGTH_SHORT).show();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (this.isVisible()) {
        // If we are becoming invisible, then...
        if (!isVisibleToUser) {
            sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
            ip.setText(sp.getString("ip",null));
            port.setText(sp.getString("port",null));
            appkey.setText(sp.getString("appkey",null));
            Log.d("MyFragment", "Not visible anymore.  Stopping audio.");
            // TODO stop audio playback
        }
    }
}

@Override
public void onStart() {
    super.onStart();
    Toast.makeText(getActivity(),"On start",Toast.LENGTH_SHORT).show();
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
    ip.setText(sp.getString("ip",null));
    port.setText(sp.getString("port",null));
    appkey.setText(sp.getString("appkey",null));
}

}

//here is my dialog in the activity

final View v=LayoutInflater.from(this).inflate(R.layout.formlayout,null);
       AlertDialog.Builder ab=new AlertDialog.Builder(this);
        ab.setView(v);
        tp= (TextInputLayout) v.findViewById(R.id.ip);
        tp1= (TextInputLayout) v.findViewById(R.id.port);
        tp2= (TextInputLayout) v.findViewById(R.id.appkey);

        ab.setTitle("Enter Connection Details");
        ab.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                String a=tp.getEditText().getText().toString();
                String b=tp1.getEditText().getText().toString();
                String c=tp2.getEditText().getText().toString();
                if(!TextUtils.isEmpty(a)&&!TextUtils.isEmpty(b) && !TextUtils.isEmpty(c))
                {
                    SharedPreferences.Editor edit=sp.edit();
                    edit.putString("ip",a);
                    edit.putString("port",b);
                    edit.putString("appkey",c);
                    edit.commit();
                }
            }
        });
        ab.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog abc=ab.create();
        abc.show();
        abc.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        });

Solution

  • abc.show();
    abc.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            fragmentInstance.refreshEditTextValue(value);
        }
     });
    

    In your fragment :

     public refreshEditTextValue(String value) {
    //TODO set value in edittext
        }