简体   繁体   中英

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. I'm never leaving the Activity; therefore onSaveInstanceState within the Fragment is never called. onPause is called, but I'm unable to figure out how to save the state within there.

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. 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. 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:

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 . Search for my answer on it. This is a safe way to pass data from Fragment to Activity . And then you can easily pass that data from Activity to the Fragment's constructor or public method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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