Tôi đã xem một số câu hỏi về SO về Fragments và tôi vẫn không thể tìm ra nếu tôi muốn làm gì, và hơn thế nữa nếu thiết kế của tôi mô hình chỉ là thiếu sót và tôi cần phải làm lại toàn bộ quá trình. Về cơ bản, giống như hầu hết các câu hỏi đã được hỏi, tôi có một ActionBar với NavigationTabs (sử dụng ActionBarSherlock), sau đó trong mỗi Tab có một FragementActivity và sau đó FragmentActivities đẩy Fragments mới khi một hàng được chọn (tôi đang cố gắng tạo lại một dự án iOS trong Android và nó chỉ là một ứng dụng dựa trên Điều hướng cơ bản với một số tab có thể đi sâu vào thông tin cụ thể). Khi tôi nhấp vào nút quay lại trên điện thoại, Fragment trước đó được tải nhưng Fragment sẽ tự tạo lại (vì vậy các dịch vụ Web được gọi lại cho mỗi chế độ xem) và điều này không cần thiết vì thông tin sẽ không thay đổi trong chế độ xem trước đó đi ngược lại. Vì vậy, về cơ bản những gì tôi muốn tìm ra là làm thế nào để tôi thiết lập Fragments của tôi để khi tôi nhấn nút quay lại trên điện thoại, Fragment trước đó chỉ được kéo lên với các mục trước đó đã được tạo ra. Dưới đây là mã hiện tại của tôi:Android Fragment, quay lại mà không cần tạo lại/tải lại Fragment
//This is from my FragmentActivity Class that contains the ActionBar and Tab Selection Control
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
int selectedTab = tab.getPosition();
if (selectedTab == 0) {
SalesMainScreen salesScreen = new SalesMainScreen();
ft.replace(R.id.content, salesScreen);
}
else if (selectedTab == 1) {
ClientMainScreen clientScreen = new ClientMainScreen();
ft.replace(R.id.content, clientScreen);
}.....
//This is within the ClientMainScreen Fragment Class, which handles moving to the Detail Fragment
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do something if Row is clicked
try{
String selectedClientName = clientObject.getString("ClientName");
String selectedClientID = clientObject.getString("ClientID");
String selectedValue = clientObject.getString("ClientValue");
transaction = getFragmentManager().beginTransaction();
ClientDetailScreen detailScreen = new ClientDetailScreen();
detailScreen.clientID = selectedClientID;
detailScreen.clientName = selectedClientName;
detailScreen.clientValue = selectedValue;
int currentID = ((ViewGroup)getView().getParent()).getId();
transaction.replace(currentID,detailScreen);
transaction.addToBackStack(null);
transaction.commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
});....
//And then this is the Client Detail Fragment, with the method being called to Call the Web Service and create thew (since what is displayed on this screen is dependent on what is found in the Web Service
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
return inflater.inflate(R.layout.clientdetailscreen, group, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Setup Preferences File Link
this.preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
//initialize the table object
mainTable = (TableLayout)getActivity().findViewById(R.id.mainTable);
//setup the detail table
setupRelatedClientSection();
}
Các Screen Khách hàng chi tiết sau đó có thể đi sâu thêm một lần nữa, bằng cách sử dụng phương pháp tương tự như Client Màn hình chính nhưng khi tôi quay trở lại từ đó màn hình mới với chi tiết màn hình seuptRelatedClientSection () được gọi lại và do đó toàn bộ Fragment được xây dựng lại khi thực sự tôi chỉ muốn kéo lên một phiên bản đã lưu của màn hình đó. Điều này có thể xảy ra với thiết lập hiện tại của tôi hay tôi đã tiếp cận sai cách này?
tôi đã sử dụng .add() thay vì .show(). Từ đoạn thứ hai, khi tôi nhấn nút quay lại, nó đã đi đến mảnh đầu tiên và không làm mới. – Sri