简体   繁体   English

将数据从子活动传递到父活动

[英]Pass data from sub activity to parent activity

I've seen this thread: Android: how to use data passed from parent activity in a sub activity? 我看过以下主题: Android:如何在子活动中使用从父活动传递的数据? and ready about this method, but I'm wondering if this is outdated. 并准备好使用此方法,但我想知道这是否已过时。 Android has made a lot of strides since then and I don't want to do this the wrong way. 从那时起,Android取得了长足进步,我不想以错误的方式进行操作。

Is this still the method that should be used? 这仍然是应该使用的方法吗?

This method is perfectly fine and is still used today. 这种方法非常好,今天仍在使用。 There is one more way in which you can pass the data using Bundle . 还有另一种方法可以使用Bundle传递数据。 Bundles can be used as follows 捆绑包可以如下使用

        Intent intent = new Intent(ActivityA.this, ActivityB.class);  
        Bundle b = new Bundle(); 
        b.putString("name", "abcd");
        b.putInt("value", 666);  
        //Add the set of extended data to the intent and start it
        intent.putExtras(b);
        startActivity(intent);  

And in the other activity use 并在其他活动中使用

    Bundle b = getIntent().getExtras(); 
    int value = b.getInt("value", 0);
    String name = b.getString("name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM