完成活动/意图
我正在制作一个程序,该程序将进入另一个活动以获取一些数据,然后通过意图将数据返回到我的主要活动。我目前拥有的代码确实打开了一个新活动,它获取并发送数据,但似乎在调用 finish() 时“重新启动”我的主要活动。
问题:如何停止我的第二个活动并重新启动我的主要活动?
主要活动:
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);
}
次要活动:
Intent intent = new Intent(FileChooser.this,AndroidVideoPlayer.class);
intent.putExtra("stringPath",intentPath1);
setResult(1,intent);
finish(); // <--- does close activity, but restarts main activity
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是它应该如何工作的。您需要覆盖 onActivityResult 方法,用于从意图中获取 stringPath。
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.
好的。您似乎在 onActivityResult 定义中缺少这一行。
Ok. You seem to be missing this line inside your onActivityResult definition.
您的活动似乎再次被重新创建。尝试覆盖
onSaveInstanceState
并在onCreate
上使用savedInstanceState
。它应该有效。It seems like your activity is being recreated again. Try overriding
onSaveInstanceState
and using thesavedInstanceState
on theonCreate
. It should work.