使用onResume清除某些活动变量

时间:2016-11-15 19:38:20

标签: android android-activity onresume

我正在创建一个包含2个活动的简单Android应用。活动B显示从A传递的一堆数据。当用户从BI按下后退按钮时需要清除一些变量以便不复制B上显示的数据,但是我在onResume()上删除的两个变量都不是& #39; t清除。这是我的代码:

public class MainActivity extends AppCompatActivity {

List<Camarera> camareras = new ArrayList<Camarera>();
List<String> resultados = new ArrayList<String>();

int camHabs = 0;

Button boton;

TextView dinero;
TextView camarera1, camarera2, camarera3, camarera4, camarera5;

EditText hora1, hora2, hora3, hora4, hora5;
EditText resta1, resta2, resta3, resta4, resta5;

Switch switch1, switch2, switch3, switch4, switch5;

@Override
public void onResume()
{  
    super.onResume();
    resultados = new ArrayList<>();
    camHabs = 0;
    Toast.makeText(getApplicationContext(),"Resumiendo", Toast.LENGTH_LONG).show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    boton = (Button) findViewById(R.id.Boton);

    camHabs = 0;

    boton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Calcular();
        }
    });
}

void Calcular(){

    dinero = (TextView) findViewById(R.id.Dinero);

    camarera1 = (TextView) findViewById(R.id.Camarera1);
    camarera2 = (TextView) findViewById(R.id.Camarera2);
    camarera3 = (TextView) findViewById(R.id.Camarera3);
    camarera4 = (TextView) findViewById(R.id.Camarera4);
    camarera5 = (TextView) findViewById(R.id.Camarera5);

    hora1 = (EditText) findViewById(R.id.C_Horas1);
    hora2 = (EditText) findViewById(R.id.C_Horas2);
    hora3 = (EditText) findViewById(R.id.C_Horas3);
    hora4 = (EditText) findViewById(R.id.C_Horas4);
    hora5 = (EditText) findViewById(R.id.C_Horas5);

    resta1 = (EditText) findViewById(R.id.C_Resta1);
    resta2 = (EditText) findViewById(R.id.C_Resta2);
    resta3 = (EditText) findViewById(R.id.C_Resta3);
    resta4 = (EditText) findViewById(R.id.C_Resta4);
    resta5 = (EditText) findViewById(R.id.C_Resta5);

    switch1 = (Switch) findViewById(R.id.C_Switch1);
    switch2 = (Switch) findViewById(R.id.C_Switch2);
    switch3 = (Switch) findViewById(R.id.C_Switch3);
    switch4 = (Switch) findViewById(R.id.C_Switch4);
    switch5 = (Switch) findViewById(R.id.C_Switch5);


    List<TextView> nombres = Arrays.asList(camarera1, camarera2, camarera3, camarera4, camarera5);
    List<EditText> horas = Arrays.asList(hora1, hora2, hora3, hora4, hora5);
    List<EditText> restas = Arrays.asList(resta1, resta2, resta3, resta4, resta5);
    List switches = Arrays.asList(switch1, switch2, switch3, switch4, switch5);

    int resultado;
    int maxHoras = 0;
    int Dinero = Integer.parseInt(dinero.getText().toString());

    for (int i = 0; i < nombres.size(); i++) {
        Camarera cam = new Camarera();

        cam.SetNombre(nombres.get(i).getText().toString());
        cam.SetHoras(horas.get(i).getText().toString());
        maxHoras += cam.GetHoras();
        cam.SetResta(restas.get(i).getText().toString());
        Switch var = (Switch) switches.get(i);
        cam.activa = var.isChecked();

        camareras.add(cam);
        }

        for (int i = 0; i < camareras.size(); i++) {
            Camarera cam = camareras.get(i);

            if (cam.activa) {
                camHabs++;
                if (cam.error) {
                    Toast.makeText(getApplicationContext(),cam.error_S, Toast.LENGTH_LONG).show();
                    return;
                }


                resultado = ((cam.GetHoras() * Dinero) / maxHoras) - cam.GetResta();
                resultados.add(cam.GetNombre() + " se lleva " + resultado + "€");
            }
    }

    Intent intent = new Intent(MainActivity.this, Tabla.class);
    intent.putExtra("lista", (ArrayList<String>) resultados);
    intent.putExtra("habs", camHabs);
    startActivity(intent);



}
你能帮帮我吗?一整天都坚持这一点。

3 个答案:

答案 0 :(得分:0)

尝试在Resume上将它们等于null,然后再重新创建已经完成的空值。如果有效,请告诉我。

答案 1 :(得分:0)

来自OnResume上的文档:

  

请注意,每当您的活动进入前台时系统都会调用此方法,包括第一次创建活动时。

当您按下主页按钮/切换应用程序时甚至会调用它,因此我认为这不是一个好主意。

为什么不在拨打startActivity电话之前执行此操作:

Intent intent = new Intent(MainActivity.this, Tabla.class);
intent.putExtra("lista", (ArrayList<String>) resultados);
intent.putExtra("habs", camHabs);

//Reset variables here
resultados = new ArrayList<>();
camHabs = 0;

startActivity(intent);

答案 2 :(得分:0)

实际上,错误不在于这两个变量,它是第三个被称为“camareras”的我没有清除它,因为我在每个循环中使用它,它会一次又一次地添加到“camHabs”。

非常感谢!