我可以在finish()上传递一个Intent Extra吗? [英] Can I pass an Intent extra on finish()?

查看:81
本文介绍了我可以在finish()上传递一个Intent Extra吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否可以在调用finish()之后将信息发送给我返回的活动?

I'm wondering, is it possible to send information to the activity that I return to after calling finish()?

例如,我有一个活动 SendMessageActivity.class ,该活动允许用户向其供稿中发布一条消息.该消息保存到服务器后,我将调用 finish().我是否应该使用新的Intent来启动 MainActivity.class ?还是只完成 SendMessageActivity.class 的生​​命周期开发更好?

For example, I have an Activity SendMessageActivity.class which allows the user to post a message to their feed. Once that message has been saved to the server, I call finish(). Should I instead just start my MainActivity.class with a new Intent? Or is it better for life cycle development to just finish SendMessageActivity.class?

我看不到开始新活动的意义,因为关闭当前活动将始终使您回到 MainActivity.class .完成当前的活动后,如何发送额外的字符串?

I don't see the point of starting a new activity since closing the current one will always bring you back to MainActivity.class. How can I just send a String extra after finishing the current Activity?

推荐答案

使用 onActivityResult .

这可能有助于您了解 onActivityResult .

通过使用 startActivityForResult(Intent intent,int requestCode),您可以启动另一个活动,然后在 onActivityResult()方法.因此 onActivityResult()是您启动另一个Activity的地方.

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();
        }
    }

注意:现在,在 startActivityForResult 中检入 MainActivity ,并在其中指定 REQUEST_CODE .假设您要调用三个不同的Activity来获取结果..因此,存在三个带有三个不同 REQUEST_CODE startActivityForResult 调用.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 ;)

这篇关于我可以在finish()上传递一个Intent Extra吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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