我正在试图找出如何将用户输入的数据保存在片段上,如果它们离开并返回。我有一个活动,几个碎片工作。我发现的所有示例都在保留离开Fragment和Activity时的状态。我永远不会离开活动;因此,永远不会调用Fragment中的onSaveInstanceState。 onPause被调用,但我无法弄清楚如何在那里保存状态。
此应用程序显示故障单列表。当用户点击票证时,它从本地数据库中获取初步信息以获取只读字段,并允许用户在提交之前输入票证信息。我正在尝试允许用户点击故障单,输入数据,离开并返回该故障单,而不会丢失输入的信息。
在我研究过的所有示例中,在我看来,我可以使用Fragmenttransaction来保存Fragment的ID。以下是所有片段附加到的主要活动:
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();
}
任何帮助将非常感谢。如果我需要发布更多代码,请告诉我。谢谢!
答案 0 :(得分:0)
我认为您应该在用户离开片段时将故障信息存储在您的“活动”中并获取信息并在您回来时显示。
在Activity_Main类中,您可以创建两个方法:
public void saveTicketInfo(TicketInfo info) {
// save info here
}
和
public TicketInfo getTicketInfo() {
// get info here
}
在你的片段中:
@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();
}
答案 1 :(得分:0)
我在SO Passing data between fragments contained in an activity上发布了一个冗长的详细答案。搜索我的答案。这是将数据从Fragment传递到Activity 的安全方式。然后,您可以轻松地将数据从Activity传递到Fragment的构造函数或公共方法。