【问题标题】:Android return data to previous activityAndroid将数据返回到以前的活动
【发布时间】:2012-11-29 14:32:33
【问题描述】:

我需要你的帮助:我想在完成当前活动之前将额外数据放入上一个活动。

例如:活动 A 启动活动 B 当我完成 Activity B 时,我想要 Activity A 中的新数据。

我该怎么做? 非常感谢之前

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

    Android SDK解释here,更好的SO问题答案+示例here

    【讨论】:

      【解决方案2】:

      使用startActivityforResult打开活动B..然后在你的活动A中覆盖onActivityResult(int, int, Intent)..

      例子:

      public class MyActivity extends Activity {
       ...
      
       static final int PICK_CONTACT_REQUEST = 0;
      
       protected boolean onKeyDown(int keyCode, KeyEvent event) {
           if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
               // When the user center presses, let them pick a contact.
               startActivityForResult(
                   new Intent(Intent.ACTION_PICK,
                   new Uri("content://contacts")),
                   PICK_CONTACT_REQUEST);
              return true;
           }
           return false;
       }
      
       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));
               }
           }
       }
      }
      

      查看http://developer.android.com/reference/android/app/Activity.html

      【讨论】:

        【解决方案3】:

        使用startActivityforResult启动Activity B。在Activity A中实现override onActivityResult(int, int, Intent)方法,在ActivityB中实现setResult。

        例子:

        public class ActivityA extends Activity {
        
        static final int REQUEST_CODE = 0;
        
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
        
                setContentView(R.layout.xyz);
        
                DateBtn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            startActivityForResult(new Intent(ActivityA.this,ActivityB.class), REQUEST_CODE);
                        }
                    });
            }
        
         protected void onActivityResult(int requestCode, int resultCode,
                 Intent data) {
             if (requestCode == REQUEST_CODE) {
                 if (resultCode == RESULT_OK) {
                     // A contact was picked.  Here we will just display it
                     // to the user.
                     startActivity(new Intent(Intent.ACTION_VIEW, data));
                 }
             }
         }
        }
        
        
        public class ActivityB extends Activity {
        
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
        
                setContentView(R.layout.xyz);
        
                BackBtn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("DATA", "your string");
                    setResult(RESULT_OK, intent);
                    finish();
                        }
                    });
            }
        
        }
        

        【讨论】:

        • 在Activity A中使用startActivityforResult启动Activity B,在Activity A中使用@override onActivityResult(int, int, Intent)方法从Activity B中获取数据。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多