简体   繁体   English

将自定义对象的 ArrayList 从子活动传回父活动

[英]Pass ArrayList of custom objects from child activity back to parent

I'm making a basic habit tracking app.我正在制作一个基本的习惯跟踪应用程序。 I have a fragment class called HabitFragment (it's a fragment because it is just one part of a multi-tabbed app) that displays a list of Habits (which are objects of a custom Habit class and contain name, frequency, etc), each of which has a "log" button.我有一个名为 HabitFragment 的片段 class(它是一个片段,因为它只是多选项卡式应用程序的一部分),它显示了一个习惯列表(它们是自定义习惯 class 的对象,包含名称、频率等),每个其中有一个“日志”按钮。 This log button takes the user to another activity called HabitLog, where they say at which date and time they performed said habit.此日志按钮将用户带到另一个名为 HabitLog 的活动,在那里他们会说出他们在哪个日期和时间执行了上述习惯。 In this Activity I have an ArrayList of custom objects which store the date and time of a habit recorded by the user, aptly called HabitDateAndTime.在这个活动中,我有一个自定义对象的 ArrayList,它存储用户记录的习惯的日期和时间,恰当地称为 HabitDateAndTime。 I want to be able to send this ArrayList back to the HabitFragment class on the press of a button.我希望能够通过按下按钮将此 ArrayList 发送回 HabitFragment class。 I can't get it to work.我无法让它工作。

I have tried this in the onCreate method of my HabitLog activity.我已经在 HabitLog 活动的 onCreate 方法中尝试过这个。 It is based on what I could find on old posts on this site:它基于我在该网站上的旧帖子中可以找到的内容:

    doneButton.setOnClickListener(v -> {
        Gson gson = new Gson();
        Intent data = new Intent();
        data.putExtra("dateTimes", gson.toJson(datesAndTimes));
        setResult(RESULT_OK, data);
        finish();
    });

and this in the onResume of HabitFragment:这在 HabitFragment 的 onResume 中:

public void onResume() {
    super.onResume();
    Gson gson = new Gson();
    Intent intent = this.getActivity().getIntent();
    String str = intent.getStringExtra("datesTimes");
    List<HabitDateAndTime> datesTimes = gson.fromJson(str, List.class);
}

But when I check the value of datesTimes, it says null even if I add things to it in the child activity.但是当我检查 datesTimes 的值时,它会显示 null 即使我在子活动中添加了一些东西。

How do I properly send an ArrayList of custom objects from a child activity to a parent?如何正确地将自定义对象的 ArrayList 从子活动发送到父活动?

Start your child Activity for result using startActivityForResult() and then in your child Activity set the result, after you have collected your data from the user, using setResult() and manually finish your child Activity using finish() .使用startActivityForResult()启动您的子Activity以获得结果,然后在您的子Activity中设置结果,在您从用户那里收集数据后,使用setResult()并使用finish()手动完成您的子Activity Then in the HabitLogFragment class again, override onActivityResult() to receive the data set in your child Activity .然后再次在HabitLogFragment class 中,覆盖onActivityResult()以接收在您的子Activity中设置的数据。

Refer to this guide for more in depth information: https://developer.android.com/training/basics/intents/result .有关更多详细信息,请参阅本指南: https://developer.android.com/training/basics/intents/result

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

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