您如何将数据从当前活动传递到上一个活动

时间:2018-10-18 22:19:28

标签: android kotlin kotlin-android-extensions

我正在尝试将值从活动3传递给活动2,但我得到的是空值。如果我单击“后退”按钮,它将转到上一个活动,但值为null。在下面添加了建议的方法代码。但仍然无法获得结果。

Suggested Approach:

Activity :3

   override fun onBackPressed() {
        sendDataBackToPreviousActivity()
        super.onBackPressed()
     }

    private fun sendDataBackToPreviousActivity()
    {
        val navBarTitle21=intent.getStringExtra(TestProjectMenuViewHolder.TEST_TITLE_NAME)
        val intent=Intent().apply { putExtra("ReturnMessage",navBarTitle21)}
        setResult(Activity.RESULT_OK,intent)
    }


    Activity:2

    Main Class:
        companion object {
        const val START_ACTIVITY_3_REQUEST_CODE = 0
    }
  val intent=Intent(this,TestProjectMenuDetail::class.java)
        startActivityForResult(intent, START_ACTIVITY_3_REQUEST_CODE)

    Declared outside Main Class:

       override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == START_ACTIVITY_3_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val message = data!!.getStringExtra("ReturnMessage")
                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
                println("Message Value: $message")
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

First Approach:
    Activity:3
    val navBarTitle= intent.getStringExtra(TestMenuViewHolder.TEST_TITLE_KEY)
        supportActionBar?.title=navBarTitle//Something Like "StackOverFlow". THis is for back Button
            TestMenuDetail:
            val navBarTitle2=intent.getStringExtra(TestMenuViewHolder.TEST_TITLE_NAME)
            val TestVar=Intent(this@TestMenuDetail,TestMenuList::class.java)
            intent.putExtra("TestVar2",navBarTitle2)
            println("Test Value $navBarTitle2")//Test Value Hello

    Activity:2      
    TestMenuList:
            val navBarTitle3=intent.getStringExtra("TestVar2")
            println("Helllo Test: $navBarTitle3")//Helllo Test: null

3 个答案:

答案 0 :(得分:1)

您应该使用startActivityForResult API来完成任务。

Activity2.kt

class Activity2 : AppCompatActivity() {
    companion object {
        const val START_ACTIVITY_3_REQUEST_CODE = 0
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity2)

        // Start Activity3
        val intent = Intent(this, Activity3::class.java)
        startActivityForResult(intent, START_ACTIVITY_3_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == START_ACTIVITY_3_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val message = data!!.getStringExtra("message")
                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}

Activity3.kt

class Activity3 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity3)
    }

    override fun onBackPressed() {
        sendDataBackToPreviousActivity()
        super.onBackPressed()
    }

    /**
     * Send data back to previous activity which start this one, you can call this method when users press on back key
     * or when users press on a view (button, image, etc) on this activity.
     */
    private fun sendDataBackToPreviousActivity() {
        val intent = Intent().apply {
            putExtra("message", "This is a message from Activity3")
            // Put your data here if you want.
        }
        setResult(Activity.RESULT_OK, intent)
    }
}

答案 1 :(得分:0)

好的,这就是我要做的:

重写onBackPressed方法,然后使用Intent在方法内部传递变量值。在活动2中,从活动3接收值。

活动3

@override
public void onBackPressed (){

    Intent intent = new Intent(getApplicationContext(), Activity2.class);

    intent.put("value_key", value);
    startActivity(intent);
}

在活动2中接收值

getIntent.getValue("value_key");

别忘了检查语法,我只是用手机写的。希望对您有帮助!

答案 2 :(得分:0)

在上一活动中接收数据后,您始终可以使用SharedPreferences,然后清除它们。这是100%有效的方法。放上:

val sharedPreference =  getSharedPreferences("prefs name",Context.MODE_PRIVATE)
var editor = sharedPreference.edit()
editor.putString("your value name","value")
editor.commit()

并获得它:

sharedPreference.getString("your value name","default value")

但是您当然必须在上一个活动中再次打开首选项;)

val sharedPreference =  getSharedPreferences("prefs name",Context.MODE_PRIVATE)