1

In MainActivity I press on a button, then a dialog (a class that extends DialogFragment) will be opened. In that dialog, I press a button and a value will be saved as SharedPrefernece. Now when I close that dialog, I want to read that saved value in MainActivity.

This is how I tried it but my code will not run when I close the dialog.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println(Helper.getSharedPreference(getApplicationContext(), "dialog"));

So how can I refresh MainActivity?

xRay
  • 543
  • 1
  • 5
  • 29

3 Answers3

0

You should have a Look in the Android lifcycle, If your Dialog is an Activity with Dialog Look n Feel, probably only Activity.onResume() will be called. If you are using a real Dialog you should use a listener instead.

As the documentation says that since API 28 DialogFragment is deprecated API, better dont use it.
Maybe better let an activity implement that fragment and let it look like an Dialog via Style. Then the casual android lifecycle should work well.
Activity to Dialog:
https://stackoverflow.com/a/1979631/8524651
Android Lifecycle:
https://stackoverflow.com/a/19485273/8524651

Hatzen
  • 408
  • 2
  • 9
  • 16
0

When user clicks on positive or negative button in DialogFragment you will get notified in respective listeners which you pass while calling setPostiveButton() and setNegativeButton() methods. From here you can notify your parent activity to refresh the data it is showing. You can check the official docs for more clarity.

Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
0

You can use this link to reference:

https://www.tutorialspoint.com/how-to-reload-activity-in-android

Jay_Panchal
  • 327
  • 2
  • 11