Android-返回具有不同意图值的上一个活动 [英] Android- Going back to Previous Activity with different Intent value

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

问题描述

我有一个具有此转换的应用程序:

I have an application that has this transition:

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

进入 C 后,我必须检查一个标志.然后我必须将它作为意图(让我们说 intentX = false)传递给 D.在 D 中做了一些事情之后,它会在按下按钮后回到 C .我所做的只是再次传递值为 true 的 intentX,然后再次 startActivity C.那么结果是它创建了另一个 Activity C.

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.

我想要发生的是,我不必启动一个新的 Activity C,而是通过调用 super.onBackPressed() 来使用以前的 C.但是我无法传递 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() 它可以包含一个 Intent 对象extras 传回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.

我不建议在 onBackPressed() 中这样做,如果你没有,因为这不是用户所期望的.相反,您应该使用诸如 Button 单击之类的事件返回此数据.

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-返回具有不同意图值的上一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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