简体   繁体   English

将片段状态/数据从片段保存到片段

[英]Saving Fragment State/Data from Fragment to Fragment

I'm trying to figure out how to save user inputted data on a fragment if they leave it and come back. 我试图弄清楚如何将用户输入的数据保存在一个片段上(如果他们离开并返回)。 I have one Activity that several Fragments work off of. 我有一个活动,需要处理多个片段。 All the examples I find are saving the state when leaving the Fragment and Activity. 我发现的所有示例在离开Fragment和Activity时都保存状态。 I'm never leaving the Activity; 我从不离开活动; therefore onSaveInstanceState within the Fragment is never called. 因此,片段中的onSaveInstanceState永远不会被调用。 onPause is called, but I'm unable to figure out how to save the state within there. 调用了onPause,但是我无法弄清楚如何在其中保存状态。

This app displays a list of tickets. 此应用程序显示票证列表。 When the user taps a ticket, it grabs preliminary information from the local DB for read only fields and allows the user to input the ticket information before submitting. 当用户点击票证时,它会从本地DB获取用于只读字段的初步信息,并允许用户在提交之前输入票证信息。 I'm trying to allow a user to tap on a ticket, enter data, leave and come back to that ticket without losing information entered. 我试图允许用户点击票证,输入数据,离开并返回该票证而不会丢失输入的信息。

Out of all the examples I've studied on this, it seems to me I can use Fragmenttransaction to save the Fragment's ID. 在我研究过的所有示例中,在我看来,我可以使用Fragmenttransaction保存Fragment的ID。 Below is the main Activity that all fragments are attached to: 以下是所有片段都附加到的主要活动:

public class Activity_Main extends Activity 
implements  Fragment_OpenTickets.INT_OnOpenTicketSelectedListener,
            Fragment_SubTickets.INT_OnSubTicketSelectedListener,
            Fragment_Ticket.INT_SubmitNewTicket {
DBController dbc;
Fragment fr;
boolean gps_enabled = false;
boolean network_enabled = false;
HashMap<String, String> addInfo;
RadioButton btn_radio_open;
RadioButton btn_radio_submitted;
RadioButton btn_radio_settings;
String ticketDate, ticketTime;
int m, d, y, hr, min;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_radio_open = (RadioButton)findViewById(R.id.radio_open);
    btn_radio_submitted = (RadioButton)findViewById(R.id.radio_submitted);
    btn_radio_settings = (RadioButton)findViewById(R.id.radio_settings);
    dbc = new DBController(this);
    if (dbc.checkCredentials() != null) {
        fr = new Fragment_OpenTickets();
    } else {
        fr = new Fragment_Settings();
        btn_radio_open.setChecked(false);
        btn_radio_submitted.setChecked(false);
        btn_radio_settings.setChecked(true);
    }
    FragmentManager fm = getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_place, fr);
    ft.addToBackStack(null); // For back button action.
    ft.commit();

    // Start scheduler service.
    this.startService(new Intent(this, Service_Send.class));
    // Start GPS service.
    startGPS();
}

/**
 * Top menu buttons.
 */
public void selectFrag(View view) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    switch (view.getId()) {
        case R.id.radio_open:
            //fr = new Fragment_OpenTickets();
            ft.replace(R.id.fragment_place, new Fragment_OpenTickets());
            ft.addToBackStack(null);
            btn_radio_submitted.setChecked(false);
            btn_radio_settings.setChecked(false);
            ft.commit();
            break;
        case R.id.radio_submitted:
            //fr = new Fragment_SubTickets();
            ft.replace(R.id.fragment_place, new Fragment_SubTickets());
            ft.addToBackStack(null);
            btn_radio_open.setChecked(false);
            btn_radio_settings.setChecked(false);
            ft.commit();
            break;
        case R.id.radio_settings:
            //fr = new Fragment_Settings();
            ft.replace(R.id.fragment_place, new Fragment_Settings());
            ft.addToBackStack(null);
            btn_radio_open.setChecked(false);
            btn_radio_submitted.setChecked(false);
            ft.commit();
            break;
    }
}

/**
 * Open ticket selection action.
 */
public void onOpenTicketSelected(HashMap position) {
    // Create fragment and give it an argument for the selected ticket.
    fr = new Fragment_Ticket();
    Bundle args = new Bundle();
    args.putSerializable("HashMap", position);
    fr.setArguments(args);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_place, fr);
    ft.addToBackStack(null); // For back button action.
    ft.commit();
}

/**
 * Submitted ticket selection action.
 */
public void onSubTicketSelected(HashMap position) {
    // Create fragment and give it an argument for the selected ticket.
    fr = new Fragment_SubTicket();
    Bundle args = new Bundle();
    args.putSerializable("HashMap", position);
    fr.setArguments(args);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.addToBackStack(null); // For back button action.
    fragmentTransaction.commit();
}

Any help will be very appreciated. 任何帮助将不胜感激。 If I need to post more code then please let me know. 如果我需要发布更多代码,请告诉我。 Thank you! 谢谢!

I think you should store ticket info in your Activity when users leave the fragment and get the info back and display it when you come back. 我认为您应该在用户离开片段时将票证信息存储在您的活动中,并在回来时将其取回并显示出来。

In Activity_Main class, you create 2 methods: 在Activity_Main类中,创建2个方法:

public void saveTicketInfo(TicketInfo info) {
    // save info here
}

And: 和:

public TicketInfo getTicketInfo() {
    // get info here
}

In your fragment: 在您的片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    ...
    // get ticket info and display it
    Activity_Main mainActivity = (Activity_Main)getActivity();
    TicketInfo info = mainActivity.getTicketInfo();
    if (info != null) {
        // handle display info here
    }
    ...
}

@Override
public void onDestroyView() {
    TicketInfo info = new TicketInfo();
    // get info from view and store in the object
    Activity_Main mainActivity = (Activity_Main)getActivity();
    mainActivity.saveTicketInfo(info)
    super.onDestroyView();
}

I posted a lengthy detailed answer on SO @ Passing data between fragments contained in an activity . 我在SO @@ 在活动中包含的片段之间传递数据时发布了冗长的详细答案。 Search for my answer on it. 搜索我的答案。 This is a safe way to pass data from Fragment to Activity . 这是将数据从Fragment传递到Activity的安全方法。 And then you can easily pass that data from Activity to the Fragment's constructor or public method. 然后,您可以轻松地将数据从Activity传递到Fragment的构造函数或公共方法。

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

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