【问题标题】:Passing result to another Activity将结果传递给另一个 Activity
【发布时间】:2015-07-28 18:59:50
【问题描述】:

我正在为 Android 编写应用程序,但在更改按钮背景时遇到了困难。我有四个活动,Activity1 是一个 TableLayout,我有三个按钮。每个 Button 都会打开另一个 Activity。我想从 Activity2 更改 Activity1 中的 Button。所以我试图通过传递一个结果来做到这一点。在 Activity2 我设置了这样的结果:

@Override
public void onClick(View v) {
        setResult(Activity1.RESULT_OK);
        finish();
    }
}

在 Activity1 我有这个代码:

          protected void onActivityResult(int requestCode, int resultCode, Intent data){
          if (resultCode == RESULT_OK)
          button1.setBackgroundResource(R.drawable.image);    
          }

因此,当用户单击 Activity2 中的 Button 时,然后在 Activity1 中,Button 会更改背景。我的问题是我只能这样做一次,而且我必须在其他活动中再做两次。我尝试执行 RESULT_OK2 但它显示错误。那么我怎样才能做更多次呢?我试图以另一种方式改变背景。在 Activity2 中,我使用了 Activity1 中的 button1,但后来我得到了 NullPointerExeption。 如果有人知道如何做到这一点,请回复!

【问题讨论】:

  • 你的意思是什么我只能做一次,我必须再做两次
  • 我必须更改 Activity1 中的其他按钮。从 Activity2 我想更改 Activity1 中的 Button1 背景。从 Activity3 我想更改 Activity1 中的 Button2 背景。因此,当我从 Activity2 向 Activity1 发送 RESULT_OK 时,我无法从 Activity3 再次发送 RESULT_OK。

标签: android android-activity


【解决方案1】:

您也可以比较 requestCode(这是您在 startActivityForResult 上传递的请求代码)

这将避免 RESULT_OK 被解释为每个活动结果。

【讨论】:

    【解决方案2】:

    我尝试做 RESULT_OK2

    RESULT_OK2 没有 Activity 常量,所以这就是您收到错误的原因

    你可以回传Intent Extras。类似的东西

    @Override
    public void onClick(View v) {
        // add the intent info
        Intent i = new Intent(); // make sure to use empty constructor
        i.putExtra("image", someVar); // might want activity const for key and someVar can be String, int, or whatever you want to use 
        setResult(Activity1.RESULT_OK, i);
        finish();
    }
    }
    

    然后在data 参数中的onActivityResult() 中检查该变量并适当地设置图像。

    Going back to previous activity

    【讨论】:

    • 感谢您的回答!这几乎是好的,但我有一个小问题。当我使用完成();在 setResult 之后,我的 Activity 在我单击按钮的那一刻返回到 Activity1。但是当我不使用finish();背景没有改变。我在finish()之后放了return;但什么也没发生。你能帮帮我吗?
    • 我想我很困惑。我以为你想回到 Activity1?
    • 对不起,我没有清楚地解释我的问题。我想返回,但只能使用返回按钮。我在 Activity2 中还有其他事情要做,我不想只有在用户单击后退按钮时才自动返回。
    • 然后保存你想要的任何东西在 i.putExtra("image", someVar);到您的onClick() 中的someVar 并将上述代码发送回@Override onBackPressed() stackoverflow.com/questions/18337536/…
    【解决方案3】:

    您可以在 SharedPreference 中存储一个值,并且根据 SharedPreference 中的值,您可以更改 button1 的背景颜色。

    在活动 2 中,

    @Override
    public void onClick(View v) {
        SharedPreferences.Editor editor = getSharedPreferences("RESULTS", MODE_PRIVATE).edit();
        editor.putInt("BUTTON1_bg", 1);
        editor.commit();
        finish();
    }
    

    在活动 1 中

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          SharedPreferences prefs = getSharedPreferences("RESULTS", MODE_PRIVATE); 
          int bg = prefs.getInt("BUTTON1_bg");
          // based on value of  bg, you can decide what background to use for button1
        }
    

    【讨论】:

      【解决方案4】:
      when you start the activity for result in android you have to pass request code and based on that request code you can make the conditions in the onActivityResultMethod.
      
      **Button 1**
      Intent i = new Intent(this, yourclass1);
      startActivityForResult(i, 1);
      
      **in yourclass1**
      setResult(RESULT_OK);
      finish();
      
      **Button 2**
      Intent i = new Intent(this, yourclass2);
      startActivityForResult(i, 2);
      
      **in yourclass2**
      setResult(RESULT_OK);
      finish();
      
      **Button 3**
      Intent i = new Intent(this, yourclass3);
      startActivityForResult(i, 3);
      
      **in yourclass3**
      setResult(RESULT_OK);
      finish();
      
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      
          if (requestCode == 1 && resultCode == RESULT_OK) {
             // change background of button 1
          }else if (requestCode == 2 && resultCode == RESULT_OK) {
             // change background of button 2
          }else if (requestCode == 3 && resultCode == RESULT_OK) {
             // change background of button 3
          }
      }
      

      【讨论】:

      • 感谢您的回答!这几乎是好的,但我有一个小问题。当我使用完成();在 setResult 之后,我的 Activity 在我单击按钮的那一刻返回到 Activity1。但是当我不使用finish();背景没有改变。你能帮忙吗?
      • 当您调用finish() 时,前台的当前活动已完成,并在finish() 之后将结果传递给调用活动。如果您有任何问题或疑问,请告诉我。
      【解决方案5】:

      您需要使用不同的请求代码启动所有三个活动。或者获取意图数据来区分。 你可以做类似的事情 从Activity1requestCode 作为开始所有活动

      startActivityForResult( <intent>,<an requestCode>);
      

      然后就可以过滤onActivityResult()中相同的requestCode为

      if(requestCode==11){
         button1.setBackgroundResource(R.drawable.image);  
      }else if(requestCode==22){
         button2.setBackgroundResource(R.drawable.image);  
      }else if(requestCode==33){
         button3.setBackgroundResource(R.drawable.image);  
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 2015-12-23
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        相关资源
        最近更新 更多