当前位置:  开发笔记 > 编程语言 > 正文

我可以在finish()上传递一个Intent Extra吗?

如何解决《我可以在finish()上传递一个IntentExtra吗?》经验,为你挑选了1个好方法。

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

例如,我有一个活动SendMessageActivity.class,该活动允许用户在其提要中发布一条消息。该消息保存到服务器后,我致电finish()。相反,我应该只是MainActivity.class以一个新的Intent开始吗?还是生命周期的发展更好SendMessageActivity.class

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



1> Charuක..:

使用onActivityResult

这可能有助于您了解onActivityResult

通过使用startActivityForResult(Intent intent, int requestCode)您可以启动另一个Activity,然后在该方法中从该Activity 接收结果。从另一个位置开始也是如此。onActivityResult()onActivityResult()

onActivityResult(int requestCode, int resultCode, Intent data)在这里检查参数。请求代码在那里可以从您获得结果的地方进行过滤。因此您可以使用他们的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

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

:现在,在检查MainActivitystartActivityForResult有你指定一个REQUEST_CODE。假设您要调用三个不同的Activity以获得结果..因此,存在三个startActivityForResult具有三个不同REQUEST_CODE的调用。REQUEST_CODE就是您在活动中指定的唯一键,用于唯一标识您的startActivityForResult呼叫。

一旦您从这些活动中收到数据,就可以检查什么是REQUEST_CODE,然后您就会知道,此结果来自此活动。

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

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有