简体   繁体   English

Android:片段堆栈的问题

[英]Android: Problems with fragment backstack

I've created a Video app with a master/detail fragment navigation. 我创建了具有主/详细片段导航功能的Video应用程序。 This app has a single activity. 这个应用程式只有一个活动。

From the list fragment you click a record icon to navigate to the Camera Preview to record video. 在列表片段中,单击一个录制图标,以导航到“摄像机预览”以录制视频。

Lets say my ListFragment is [1], my camera preview fragment is [2] and my detail fragment is [3]. 可以说我的ListFragment是[1],我的相机预览片段是[2],细节片段是[3]。 Right now my navigation is [1] - [2] - [3]. 现在,我的导航是[1]-[2]-[3]。

Now when the user is 3 and presses the back button, I believe the app is trying to naviagate back to [2] and the app crashes. 现在,当用户为3并按下“后退”按钮时,我相信该应用程序正试图将导航导航回[2],并且该应用程序崩溃了。 I want to the app to always navigate back to [1] when the user presses the back button. 我希望应用程序在用户按下“后退”按钮时始终导航回[1]。

I have played around with different combinations of addToBackStack and nothing seems to be working. 我玩过addToBackStack的不同组合,但似乎没有任何效果。 Please help if you can! 如果可以的话请帮忙!

All this code is from the main activity: 所有这些代码均来自主要活动:

When first entering the app (opens the [1]): 首次进入应用程序时(打开[1]):

VideoListFragment videoListFragment = new VideoListFragment();

            videoListFragment.setArguments(getIntent().getExtras());

            getFragmentManager().beginTransaction()
                    .add(R.id.container, videoListFragment)
                    .commit();

When navigating to [2]: 导航至[2]时:

CameraFragment cameraFragment = new CameraFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.container, cameraFragment)
                .commit();

When navigating to [3]: 导航至[3]时:

VideoDetailFragment videoDetailFragment = new VideoDetailFragment();
        Bundle args = new Bundle();
        args.putString(VideoDetailFragment.UUID, String.valueOf(uuid));
        videoDetailFragment.setArguments(args);
        getFragmentManager().beginTransaction()
                .replace(R.id.container, videoDetailFragment, "VideoDetailFragment")
                .addToBackStack(null)
                .commit();

Update: Ok I've got the navigation working by overriding the back button before with this method in the main activity: 更新:好的,我已经通过在主要活动中使用此方法覆盖后退按钮来使导航正常工作:

 @Override
    public void onBackPressed() {
        if (getFragmentManager().findFragmentByTag("VideoDetailFragment") != null) {
            // I'm viewing VideoDetailFragment
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new VideoListFragment())
                    .addToBackStack(null)
                    .commit();
        } else {
            super.onBackPressed();
        }
    }

but now once I'm back in the ListFragment when I press the back button it does nothing? 但是现在当我按返回按钮返回ListFragment时,它什么都不做? I can't get out of the app? 我无法退出应用程序?

You said you tried different combinations, but it seems to me like you should call addToBackStack when navigating to [2] and not to [3]. 您说过尝试了不同的组合,但是在我看来,导航至[2]而不是[3]时应调用addToBackStack

In response to your update - You can customize the back button by checking 响应您的更新-您可以通过选中自定义后退按钮

int depth = getFragmentManager().getBackStackEntryCount();
if (depth == 0)
    // exit app
else
    // switch fragments

EDIT: Noticed you are using the FragmentManager directly and not the support fragment manager 编辑:注意到您正在直接使用FragmentManager而不是支持片段管理器

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

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