简体   繁体   English

用正确的片段恢复活动

[英]Restore activity with right fragment

I have a main activity that holds fragments. 我的主要活动是保存片段。 If I press home button and then re-enter my app, I want to restore the fragments backstack as I had never left. 如果按下主屏幕按钮,然后重新进入我的应用程序,我想恢复片段,因为我从未离开过。 Is this possible? 这可能吗? I may i do it? 我可以吗?

I don't understand pretty well your question but if you want to come back to the previous fragment, you have to use addToBackStack(null). 我不太清楚您的问题,但是如果您想回到上一个片段,则必须使用addToBackStack(null)。

Imagine that you are in one fragment: FragmentA and you call to the Fragment B 假设您在一个片段中:FragmentA,然后调用片段B

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.replace(R.id.fragments_container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_NONE);

        ft.addToBackStack(null);

    ft.commit();

If you don't add the line ft.addToBackStack(null), when the user press the back button from Fragment B, you never are going to come back to Fragment A. But if you use this line, mean that you are going to include the Fragment A in the queue of fragments before to pass to Fragment B. In that way, when the back button is pressed, you will come back to Fragment A. 如果您不添加ft.addToBackStack(null)行,则当用户按下Fragment B中的“后退”按钮时,您将永远不会回到FragmentA。但是,如果您使用此行,则意味着您将在将片段A放入片段队列之前,将其传递给片段B。这样,当按下后退按钮时,您将返回片段A。

Was that your question? 那是你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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