Terminar una actividad / Intención

I am making a that program is going into another activity to get some data, and then returning the data through intent to my main activity. The code I have at the moment does open a new activity, it gets and sends the data but seems to 'restart' my main activity when finish() is called.

Question: How do I stop my second activity restarting my main activity?

Actividad principal:

Intent intent = new Intent(AndroidVideoPlayer.this, FileChooser.class);
intent.putExtra("dir", 1);
startActivityForResult(intent, requestCode);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("CheckStartActivity","onActivityResult and resultCode = "+resultCode);
    // TODO Auto-generated method stub

    myPath = data.getStringExtra("stringPath");
    textEmpty.setText(myPath);
    myUri = Uri.parse(myPath);      
    mp = MediaPlayer.create(this, myUri);   
}

Secondary Activity:

Intent intent = new Intent(FileChooser.this,AndroidVideoPlayer.class);
intent.putExtra("stringPath",intentPath1);
setResult(1,intent);
finish(); // <--- does close activity, but restarts main activity

preguntado el 28 de agosto de 11 a las 02:08

when you finish an activity wants to go back to previous activity with result, do not create new intent, you should call getIntent; Intent intent = getIntent(); set the results as you did and finish -

3 Respuestas

That's how it is supposed to work. You need to override onActivityResult method of your main activity to get the stringPath from the intent.

Respondido 28 ago 11, 07:08

@Jon So you're saying that onActivityResult is never called? - Konstantin Burov

to add to Konstantin's answer, see another example here: stackoverflow.com/questions/1124548/… - gmjordania

Ok. You seem to be missing this line inside your onActivityResult definition.

super.onActivityResult(requestCode, resultCode, data);

Respondido 29 ago 11, 01:08

I want it to return to the previous activity, but it seems to restart the activity rather than just go back to it because all of my interface gets reset. - Jonno

It seems like your activity is being recreated again. Try overriding onSaveInstanceState y usando el savedInstanceState al onCreate. Deberia de funcionar.

contestado el 27 de mayo de 12 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.