I have an activity that lets the user to insert data from a custom dialog and a listview that display the data. After inputting the data in the dialog, the onResume is supposed to update the listview. The issue is that when the app directs the user to the activity, it calls both the onCreate and onResume, and it does not call the onResume after the user insert data from the dialog. What went wrong and how to solve it?
SubjectListActivity.java
public class SubjectListActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "SubjectListActivity";
private Animation rotateOpen;
private Animation rotateClose;
private Animation fromBottom;
private Animation toBottom;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Toolbar toolbar;
private boolean clicked = false;
private ListView lv_subjectList;
private FloatingActionButton btn_subjectMenu, btn_subjectDelete, btn_subjectAdd;
DatabaseHelper databaseHelper;
ArrayList<SubjectListModel> arrayList;
SubjectListAdapter subjectListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_list);
intViews();
loadAnimation();
databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>();
loadListView();
setSupportActionBar(toolbar);
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_home);
btn_subjectAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
Log.i(TAG, "onCreate: ");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume: ");
loadListView();
navigationView.setCheckedItem(R.id.nav_subjectList);
}
public void loadListView () {
arrayList = databaseHelper.getAllSubjectListData();
subjectListAdapter = new SubjectListAdapter(this, arrayList);
lv_subjectList.setAdapter(subjectListAdapter);
}
private void intViews() {
toolbar = findViewById(R.id.toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
btn_subjectAdd = findViewById(R.id.btn_subjectAdd);
btn_subjectMenu = findViewById(R.id.btn_subjectMenu);
btn_subjectDelete = findViewById(R.id.btn_subjectDelete);
lv_subjectList = findViewById(R.id.lv_SubjectList);
}
public void openDialog () {
SubjectListDialog subjectListDialog = new SubjectListDialog();
subjectListDialog.show(getSupportFragmentManager(), "subject list dialog");
}
}
SubjectListDialog.java
public class SubjectListDialog extends AppCompatDialogFragment {
private EditText et_subjectCode, et_subjectName, et_creditHour;
DatabaseHelper databaseHelper;
private ListView lv_SubjectList;
SubjectListAdapter subjectListAdapter;
SubjectListActivity subjectListActivity;
ArrayList<SubjectListModel> arrayList;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
databaseHelper = new DatabaseHelper(getContext());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_subjectlist_dialog, null);
builder.setView(view)
.setTitle("Add Subject")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
insert();
SubjectListDialog.this.dismiss();
}
});
et_subjectCode = view.findViewById(R.id.et_subjectCode);
et_subjectName = view.findViewById(R.id.et_subjectName);
et_creditHour = view.findViewById(R.id.et_creditHour);
return builder.create();
}
public void insert() {
boolean result = databaseHelper.insertSubjectList(et_subjectCode.getText().toString(), et_subjectName.getText().toString(),Integer.parseInt(et_creditHour.getText().toString()));
if (result) {
Toast.makeText(requireContext(), "Data has been added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(requireContext(), "Please insert all the fields correctly", Toast.LENGTH_SHORT).show();
}
}
}
Once you are done taking input you should dismiss the dialog. In your case after calling insert()
call SubjectListDialog.this.dismiss()
Update - The dialog fragment does not pauses the activity it is just like a fragment is inflated inside the view bounds of the activity itself. This can be further confirmed with the fact that the it is added to screen using the activity's own fragment manager as done in your code subjectListDialog.show(getSupportFragmentManager(), "subject list dialog");