完成活动/意图

发布于 2024-12-01 21:40:12 字数 951 浏览 0 评论 0原文

我正在制作一个程序,该程序将进入另一个活动以获取一些数据,然后通过意图将数据返回到我的主要活动。我目前拥有的代码确实打开了一个新活动,它获取并发送数据,但似乎在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书信已泛黄 2024-12-08 21:40:12

这就是它应该如何工作的。您需要覆盖 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.

无声静候 2024-12-08 21:40:12

好的。您似乎在 onActivityResult 定义中缺少这一行。

super.onActivityResult(requestCode, resultCode, data);

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

super.onActivityResult(requestCode, resultCode, data);

后eg是否自 2024-12-08 21:40:12

您的活动似乎再次被重新创建。尝试覆盖 onSaveInstanceState 并在 onCreate 上使用 savedInstanceState。它应该有效。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文