Coder Social home page Coder Social logo

activityresult's Introduction

ActivityResult

更方便的处理页面返回的结果

以往我们调用相机拍照或跳到别的页面获取一些数据时往往是这样的

   val intent = Intent(this, Main2Activity::class.java)
   startActivityForResult(intent)

然后在onActivityResult里处理结果

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK)
            Toast.makeText(this, "收到结果:${data?.getStringExtra("data")}", Toast.LENGTH_SHORT)
                    .show()
    }

这样的代码失去了连贯性 阅读起来也很困难

使用ActivityResult的代码看起来是这样的

    val intent = Intent(this, Main2Activity::class.java)
    startActivityForResult(intent)
               .subscribe {
                   if (it.isOk)
                       Toast.makeText(this, "收到结果:${it.data?.getStringExtra("data")}", Toast.LENGTH_SHORT)
                               .show()
               }

或者这样的

    val intent = Intent(this, Main2Activity::class.java)
    startActivityForResult(intent, 1)
    waitingActivityResult(1)
            .subscribe {
                if (it.isOk)
                    Toast.makeText(this, "收到结果:${it.data?.getStringExtra("data")}", Toast.LENGTH_SHORT)
                            .show()
            }

逻辑上非常连贯

activityresult's People

Contributors

limitlesser avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.