Java,Android:onActivityResult在一个活动中具有不同的参数 [英] java, android: onActivityResult with different parameters in one activity

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

问题描述

我有一个活动,其中我使用了两个onActvityResults

I have one activity where i use two onActvityResults

CalendarView的一个:

@Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) 

一个用于检索Google日历中的事件的

one for the retrieving of events in Google Calendar:

    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)

我如何将这两个活动合而为一?谢谢!

How do i have these two in one activity? Thanks!

推荐答案

似乎您没有正确理解这个概念!这可能有助于您了解 onActivityResult .

It seems you have not properly understood the concept! This might help you to understand onActivityResult.

通过使用startActivityForResult(Intent intent, int requestCode),您可以开始另一个活动,然后通过onActivityResult()方法从该活动中接收结果.因此,onActivityResult()是您所在的位置开始另一个活动.

By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.

onActivityResult(int requestCode, int resultCode, Intent data)在此处检查参数.请求代码在那里可以从您获得结果的地方进行过滤.这样您就可以使用它们的requestCodes识别不同的数据!

onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!

示例

 public class MainActivity extends Activity {

        // Use a unique request code for each use case 
        private static final int REQUEST_CODE_EXAMPLE = 0x9988; 

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Create an Intent to start AnotherActivity
            final Intent intent = new Intent(this, AnotherActivity.class);

            // Start AnotherActivity with the request code
            startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
        }

        //-------- When a result is returned from another Activity onActivityResult is called.--------- //
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            // First we need to check if the requestCode matches the one we used.
            if(requestCode == REQUEST_CODE_EXAMPLE) {

                // The resultCode is set by the AnotherActivity
                // By convention RESULT_OK means that what ever
                // AnotherActivity did was successful
                if(resultCode == Activity.RESULT_OK) {
                    // Get the result from the returned Intent
                    final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);

                    // Use the data - in this case, display it in a Toast.
                    Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
                } else {
                    // AnotherActivity was not successful. No data to retrieve.
                }
            }
        }
    }

AnotherActivity<-这是我们用来向MainActivity

AnotherActivity <- This the the one we use to send data to MainActivity

public class AnotherActivity extends Activity {

        // Constant used to identify data sent between Activities.
        public static final String EXTRA_DATA = "EXTRA_DATA";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_another);

            final View button = findViewById(R.id.button);
            // When this button is clicked we want to return a result
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Create a new Intent as container for the result
                    final Intent data = new Intent();

                    // Add the required data to be returned to the MainActivity
                    data.putExtra(EXTRA_DATA, "Some interesting data!");

                    // Set the resultCode to Activity.RESULT_OK to 
                    // indicate a success and attach the Intent
                    // which contains our result data
                    setResult(Activity.RESULT_OK, data); 

                    // With finish() we close the AnotherActivity to 
                    // return to MainActivity
                    finish();
                }
            });
        }

        @Override
        public void onBackPressed() {
            // When the user hits the back button set the resultCode 
            // to Activity.RESULT_CANCELED to indicate a failure
            setResult(Activity.RESULT_CANCELED);
            super.onBackPressed();
        }
    }

注意:现在,在MainActivity中签入startActivityForResult,在其中指定REQUEST_CODE.假设您要调用三个不同的活动来获得结果..因此,有三个startActivityForResult调用具有三个不同的 REQUEST_CODE . REQUEST_CODE就是您在活动中指定的唯一键,用于唯一标识您的startActivityForResult呼叫.

Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.

一旦您从这些活动中收到数据,就可以检查什么是REQUEST_CODE,那么您知道啊哈,这个结果就是来自此活动.

Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.

这就像您将带有彩色封面的邮件发送给您的恋人,并要求他们在相同的封面中进行答复.然后,如果您收到他们的回信,您就会知道是谁给您寄了那封信. awww;)

It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)

这篇关于Java,Android:onActivityResult在一个活动中具有不同的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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