Finishing an activity / Intent
I am making a that program is going into an开发者_Go百科other 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
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.
Ok. You seem to be missing this line inside your onActivityResult definition.
super.onActivityResult(requestCode, resultCode, data);
It seems like your activity is being recreated again. Try overriding onSaveInstanceState
and using the savedInstanceState
on the onCreate
. It should work.
精彩评论