片段返回导航

时间:2018-10-17 14:20:34

标签: android android-fragments android-activity back-stack android-navigation

我解释了以下情况:

Activity1(片段1)-> Activity1(片段2)-> Activity1(片段3)-> Activity2(片段4)(确定按钮)

如果我在活动2的fragment4上单击“确定”按钮,我应该落在Activity1(fragment2)上,然后在片段2上单击“后退”按钮,它应该落在fragment1上。

我尝试了以下代码:

activity?.supportFragmentManager?.popBackStack() 

但是它不起作用。有关如何解决此问题的任何帮助或建议。您可能会问为什么我在这里使用很多片段,但是我真的没有其他选择。

1 个答案:

答案 0 :(得分:0)

当您单击OK中的Activity2(fragment4)按钮时,只需在调用sharedPreference意图之前在Activity1中输入以下内容即可:

SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(key, "1");
editor.commit();

然后致电您的Activity1

Intent Activity1 = new Intent(getActivity(), Activity1.class);
startActivity(Activity1);

现在在您的Activity1中,在设置内容视图之前,只需检查共享首选项中是否存储了变量key,变量1中存储了值1。如果它包含值frm,则将fragment2加载到帧布局sharedPreferences = getActivity().getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedPreferences.contains(city)) { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frm,new fragment2()).addToBackStack(null).commit(); } 中。像这样:

1

正常调用此活动时,共享首选项在key中不会具有值0,因此fragment1将作为常规流加载。

现在,当您从此处移至下一个片段时,只需将key存储在共享首选项的onDetach()变量中。这可以通过fragment2

private void GetParseMime() { byte[] beginPattern = new byte[0]; byte[] endPattern = new byte[0]; WebClient webClient = new WebClient(); webClient.Headers["Accept"] = textBoxMimeType.Text; bool bBeginSeqFound = false; long savedBytesRead = 0; var uri = new Uri(url); using (Stream webStream = webClient.OpenRead(uri)) { long finalContentLen = long.Parse(webClient.ResponseHeaders["Content-Length"]); using (FileStream fileStream = new FileStream(outputFilename, FileMode.Create)) { var buffer = new byte[32768]; int bytesRead; long bytesTotal = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]); while ((bytesRead = webStream.Read(buffer, 0, buffer.Length)) > 0) { int len = bytesRead; //look for the start sequence if (!bBeginSeqFound) { int i = IndexOfSequence(buffer, dcmBeginPattern, 0); if (i > -1) { bBeginSeqFound = true; buffer = buffer.Skip(i).Take(bytesRead - i).ToArray(); len = bytesRead - i; } } else { //look for end sequence int i = IndexOfSequence(buffer, dcmEndPattern, 0); if (i > -1) { buffer = buffer.Skip(0).Take(i).ToArray(); len = buffer.Length; } } savedBytesRead += len; fileStream.Write(buffer, 0, len); } } } } 方法来完成

还可以返回fragment1,您可以按照this的答案获得更详细的解决方案。

相关问题