简体   繁体   English

android返回值意图的最佳实践

[英]best practice for intents in return value android

i have 2 activities, one is a Database of exercises and the other one is new exercise creator 我有2个活动,一个是练习数据库,另一个是新练习创建者

in this function i create a new class of an exercise, i want to return the new class to the Database activity. 在此函数中,我创建了练习的新类,我想将新类返回到数据库活动。

and when creating a new exercise delete this activity and create a new one which will create a new Exercise class. 创建新练习时,请删除此活动并创建一个新活动,这将创建一个新的Exercise类。

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);
}

what is a the best practice for 2 activities to create a class, save it to a list and create a new one when pressing the "create new exercise button" again? 再次按下“创建新锻炼按钮”创建两个班级,将其保存到列表并创建一个新活动的最佳实践是什么?

or maybe i should create the instance of the class inside the add exercise activity? 还是我应该在添加运动活动中创建该类的实例?

First of all, you're creating objects, not classes. 首先,您正在创建对象,而不是类。

You can stop Activities using finish(); 您可以使用finish();停止活动finish(); which should return you to your previous activity. 这应该使您返回上一个活动。

If you want your second Activity to send Data to the first activity maybe check out startActivityForResult . 如果您希望第二个活动将数据发送到第一个活动,则可以查看startActivityForResult Check out this topic. 查看主题。

From your FirstActivity call the SecondActivity using startActivityForResult() method 从您的FirstActivity中,使用startActivityForResult()方法调用SecondActivity

For example: 例如:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

In your SecondActivity set the data which you want to return back to FirstActivity. 在您的SecondActivity中,设置要返回到FirstActivity的数据。 If you don't want to return back, don't set any. 如果您不想返回,请不要进行任何设置。

For example: 例如:

In secondActivity if you want to send back data: 如果要发送回数据,请在secondActivity中:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

If you don't want to return data: 如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();

Now in your FirstActivity class write following code for the onActivityResult() method. 现在,在FirstActivity类中,为onActivityResult()方法编写以下代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data) 受保护的void onActivityResult(int requestCode,int resultCode,Intent数据)

{

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM