3

This is how it looks at first:

The image description of the first image

This is the dialog fragment that pops when "edit" is pressed and I want The change to be seen in the activity after the dialog fragment is dismissed. The image description of the second image

public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = getActivity().getLayoutInflater().inflate(R.layout.edit_profile_dialog, new LinearLayout(getActivity()), false);
        editProfile = (EditText) view.findViewById(R.id.changeProfile);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        setupProgressDialog();
/*get value from Bundle*/
        String editValue = getArguments().getString("value", "");
        editProfile.setText(editValue);
        String title = getArguments().getString("title", "");
        builder.setTitle(title);
        builder.setView(view);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
/*edit the value in shared preference*/
                sharedPref = getActivity().getSharedPreferences(getString(R.string.sharedPref), 0);
                editor = sharedPref.edit();
                editor.putString(getArguments().getString("saved", ""), editProfile.getText().toString());
                editor.apply();
               ID= sharedPref.getString("id", null);
                access_token=sharedPref.getString("token",null);
//Start of AsyncTask after this
StepUp
  • 36,391
  • 15
  • 88
  • 148
Nishan Khadka
  • 385
  • 1
  • 2
  • 14

3 Answers3

1

If you only need to update the data which user inputs from your dialog you do not have to redraw whole layout.

You can only set the user name to the related textview and dismiss dialog fragment.

TextView yourNameTextView = (TextView)findViewById(R.id.your_textview);

public void setNameToTextView(String name){
    yourNameTextView.setText(name);
}

And when user clicks to Ok button you can call:

((YourActivity)getActivity).setText(input);

Good luck.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • 1
    It's not a good pattern for your fragment to deal directly with one specific activity. For this, prefer use contract pattern like this : https://gist.github.com/JakeWharton/2621173 – Crisic Aug 09 '18 at 19:59
  • 1
    you're right but i think this is a quick and valid solution for question. Not the best i accept. :) – savepopulation Aug 10 '18 at 10:51
0

In your dialog's onClickListener you should be able to invalidate the layout and force a redraw / refresh

check this: How to force an entire layout View refresh?

Community
  • 1
  • 1
Danny Beaumont
  • 356
  • 1
  • 8
0

Thanks to all of you guys trying to help me out.I think I got the answer by doing this: In my DialogFragment

public class DialogFragmentEditProfile extends DialogFragment {
...
/*Initialize Parent Activity*/
private ChangeProfileActivity cp;
/*Override onAttachMethod */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        cp = (ChangeProfileActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement FeedbackListener");
        }
    }
/*create a method to recreate the parent activity*/
public void onButtonPushed(View view) {
cp.recreate();
    }

Then onPostExecute() Method of AsyncTask with in the DialogFragment

  protected void onPostExecute(String result) {
            super.onPostExecute(result);
...
/*Recreate activity after successful update by calling the onButtonPushed() method*/
 onButtonPushed(getView());
}

}

Nishan Khadka
  • 385
  • 1
  • 2
  • 14