4

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?

Main Activity:

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
AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Jonno
  • 1,542
  • 3
  • 22
  • 39
  • 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 – Charlie Wu Sep 23 '15 at 02:11

3 Answers3

3

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.

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
0

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

super.onActivityResult(requestCode, resultCode, data);

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • 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 Aug 28 '11 at 15:10
0

It seems like your activity is being recreated again. Try overriding onSaveInstanceState and using the savedInstanceState on the onCreate. It should work.

EdChum
  • 376,765
  • 198
  • 813
  • 562