当应用将用户引导至 activity 时调用 onCreate 和 onResume
onCreate and onResume were called when app direct user to an activity
我有一个 activity 允许用户从自定义对话框和显示数据的列表视图中插入数据。在对话框中输入数据后,onResume 应该更新列表视图。问题在于,当应用将用户定向到 activity 时,它会同时调用 onCreate 和 onResume,并且在用户从对话框插入数据后不会调用 onResume。出了什么问题以及如何解决?
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();
}
}
}
完成输入后,您应该关闭对话框。在您的情况下,在调用 insert()
后调用 SubjectListDialog.this.dismiss()
Update - 对话框片段不会暂停 activity 它就像片段在 activity 本身的视图边界内膨胀一样。这可以通过使用 activity 自己的片段管理器添加到屏幕这一事实得到进一步证实,正如您在代码 subjectListDialog.show(getSupportFragmentManager(), "subject list dialog");
中所做的那样
我有一个 activity 允许用户从自定义对话框和显示数据的列表视图中插入数据。在对话框中输入数据后,onResume 应该更新列表视图。问题在于,当应用将用户定向到 activity 时,它会同时调用 onCreate 和 onResume,并且在用户从对话框插入数据后不会调用 onResume。出了什么问题以及如何解决?
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();
}
}
}
完成输入后,您应该关闭对话框。在您的情况下,在调用 insert()
后调用 SubjectListDialog.this.dismiss()
Update - 对话框片段不会暂停 activity 它就像片段在 activity 本身的视图边界内膨胀一样。这可以通过使用 activity 自己的片段管理器添加到屏幕这一事实得到进一步证实,正如您在代码 subjectListDialog.show(getSupportFragmentManager(), "subject list dialog");