Android-让我们再回到previous活动有不同的意图价值 [英] Android- Going back to Previous Activity with different Intent value

查看:158
本文介绍了Android-让我们再回到previous活动有不同的意图价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了这个过渡的应用程序:

I have an application that has this transition:

 A -> B -> C -> D-> C

在进入 C ,我要检查一个标志。然后,我必须把它当作目的(比方说 intentX = FALSE )以 D 。做一些在 D 后,将pressing一个按钮后,再回去 C 。 我所做的只是再次传递了 intentX 值为true,一次startActivity℃。 那么什么发生的是,它创造了另一个活动℃。

Upon entering C , i have to check a flag. Then I have to pass it as intent (let us say intentX = false) to D. After doing something in D , it will then go back to C after pressing a button. What i did was just pass again the intentX with value true, then startActivity C again. So what happen is that it created another Activity C.

我希望发生的是,我不会开始一个新的活动C,但使用previous下通过只调用 super.onBack pressed()。但我不能通 intentX 的新值。有没有其他办法,来达到我想要的。我可能会错过一些。

What i want to happen is that i will not have to start a new Activity C, but use the previous C by just calling super.onBackPressed(). But I cannot pass the new value of the intentX. Is there other way, to achieve what i want. I might have missed some.

推荐答案

你想要的是 startActivityForResult()。当您从 C D ,而不是使用去 startActivity()改用 startActivityForResult()。然后,当你想从 D 返回 C 您使用的setResult()可包括意图演员回传给 C

What you want is startActivityForResult(). When you go from C to D, instead of using startActivity() use instead startActivityForResult(). Then when you want to return from D to C you use setResult() which can include an Intent object with extras to pass back to C.

我不建议在 onBack pressed这样做()如果你不这样做的有无的到,因为这不会是用户期望的结果。相反,你应该用这个数据与事件返回,如按钮点击。

I don't recommend doing this in onBackPressed() if you don't have to because this will not be what the user expects. Instead, you should return with this data with an event such as a Button click.

所以,在 C 你会做这样的事情

So, in C you will do something like

 Intent i = new Intent(new Intent(C.this, D.class);
 startActivityForResult(i, 0);

然后在 D 当你准备返回

 Intent i = new Intent();
 i.putExtra();  // insert your extras here
 setResult(0, i);

那么当您返回到 C 你会进入这个方法(的从文档)采取

then when you return to C you will enter this method (taken from the Docs)

protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == PICK_CONTACT_REQUEST) {
         if (resultCode == RESULT_OK) {
             // A contact was picked.  Here we will just display it
             // to the user.
             startActivity(new Intent(Intent.ACTION_VIEW, data));

             /* 
                can also get the extra sent back through data
                using data.getStringExtra("someKey"); 
                assuming the extra was a String
             */

         }

这篇关于Android-让我们再回到previous活动有不同的意图价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆