我有 2 项 Activity ,一项是练习数据库,另一项是新练习创建器
在这个函数中,我创建了一个新的练习类,我想将新类返回给 数据库 Activity 。
创建新练习时删除此 Activity 并创建一个新 Activity ,这将创建一个新的练习类(class)。
public void onClickDone(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_name);
String name = editText.getText().toString();
editText = (EditText) findViewById(R.id.edit_description);
String description = editText.getText().toString();
editText = (EditText) findViewById(R.id.edit_reptitons);
EditText editSets = (EditText) findViewById(R.id.edit_sets);
EditText editTimeBetweenSets = (EditText) findViewById(R.id.edit_time_between_sets_);
int reptitons = 0 ;
int sets = 0 ;
int timeBetweenSets = 0;
try
{
reptitons = Integer.parseInt(editText.getText().toString());
sets = Integer.parseInt(editSets.getText().toString());
timeBetweenSets = Integer.parseInt(editTimeBetweenSets.getText().toString());
}
catch(NumberFormatException ex)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("please check you input");
dlgAlert.setTitle("Wrong Input");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
_excercise = new Exercise(name, description, reptitons, sets, timeBetweenSets);
}
对于 2 个 Activity 来说,创建一个类(class)、将其保存到列表并在再次按下“创建新练习按钮”时创建一个新类(class)的最佳实践是什么?
或者也许我应该在添加练习 Activity 中创建类的实例?
最佳答案
首先,您正在创建对象,而不是类。
您可以使用 finish();
停止 Activity ,这将使您返回到之前的 Activity 。
如果您希望第二个 Activity 向第一个 Activity 发送数据,可以查看 startActivityForResult
。查看this主题。
关于java - 返回值 android 中 Intent 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095759/