Android:如何在不破坏先前片段状态的情况下启动新片段?

时间:2015-11-05 14:00:54

标签: android android-fragments android-activity android-viewpager

如何将Fragment_1 Fragment_2替换为返回状态和Fragment_1(滚动,输入,表单,其他)的能力?

enter image description here

如果使用“活动”,则可以使用方法startActivityForResult。这种方法的工作完美地显示了当前任务的碎片。

我不想保存值然后替换它们。我们需要将Fragment_1保持不变。

如何在新fragment_N开启时以及使用backstack转动屏幕时保存先前片段的状态?

是否有用于执行这些任务的现成库?

2 个答案:

答案 0 :(得分:1)

您可以添加新片段并隐藏当前片段,而不是替换片段。

像这样的东西

FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.commit();

它不会丢失隐藏片段的状态

答案 1 :(得分:0)

FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.setCustomAnimations(R.anim.open_enter,R.anim.close_exit,R.anim.open_enter,R.anim.close_exit)

t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
t.setTrans
t.commit();

FragmentPagerAdapter替换为HorizontalScrollViewFrameLayout包含fragment fragment。您可以FrameLayout中的 BOOL GetProcessImageNameFromPID::getProcessNameFromProcessID(DWORD processId, WCHAR**processName) { HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; DWORD dwPriorityClass; // Take a snapshot of all processes in the system. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == INVALID_HANDLE_VALUE) { printError(TEXT("CreateToolhelp32Snapshot (of processes)")); return(FALSE); } // Set the size of the structure before using it. pe32.dwSize = sizeof(PROCESSENTRY32); // Retrieve information about the first process, // and exit if unsuccessful if (!Process32First(hProcessSnap, &pe32)) { printError(TEXT("Process32First")); // show cause of failure CloseHandle(hProcessSnap); // clean the snapshot object return(FALSE); } // Now walk the snapshot of processes, and // display information about each process in turn int i = 0; do { WCHAR*allprocessName = pe32.szExeFile; //_tprintf( TEXT("\n%d)PROCESS NAME: %s"), i, allprocessName); // Retrieve the priority class. dwPriorityClass = 0; hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); if (hProcess == NULL) printError(TEXT("OpenProcess")); else { dwPriorityClass = GetPriorityClass(hProcess); if (!dwPriorityClass) printError(TEXT("GetPriorityClass")); CloseHandle(hProcess); } DWORD pid = pe32.th32ProcessID; //_tprintf( TEXT("\n Process ID = %d"), pid ); if (pid == processId) { *processName = allprocessName; //_tprintf( TEXT("Inside Method:\n")); _tprintf(TEXT("PROCESS NAME: %s\n\n"), *processName); return TRUE; } i++; } while (Process32Next(hProcessSnap, &pe32)); CloseHandle(hProcessSnap); return(FALSE); } int _tmain(int argc, _TCHAR* argv[]) { WCHAR**processName = (WCHAR**)malloc(sizeof(WCHAR)); GetProcessImageNameFromPID::getProcessNameFromProcessID(4, processName); _tprintf(TEXT("PROCESS NAME: %s\n\n"), *processName); // correct GetProcessImageNameFromPID::getProcessNameFromProcessID(executionProcessID, processName); wstring ws(*processName); string str(ws.begin(), ws.end()); processImageName = str; cout << processImageName << endl; // some wrong characters are printed } 自由更改。