简体   繁体   English

当您具有2个片段的单个活动时,将片段添加到addToBackstack

[英]Adding Fragment to the addToBackstack when you have a single activity with 2 fragments

Android Studio 1.1.2

Hello, 你好,

I have MainActivity, TitlesFragment, and DetailsFragment. 我有MainActivity,TitlesFragment和DetailsFragment。

My MainActivity that will display TitlesFragment and when the user clicks a title the TitlesFragment will be replaced with the DetailsFragment. 我的MainActivity将显示TitlesFragment,当用户单击标题时,TitlesFragment将替换为DetailsFragment。 That works fine. 很好 However, When I add the DetailsFragment to the addToBackStack. 但是,当我将DetailsFragment添加到addToBackStack时。 When I click the back button, instead of displaying the TitlesFragment it will close the app. 当我单击“后退”按钮时,它不会关闭TitlesFragment而是显示该应用程序。

I am thinking for I need 2 Activity for this. 我在想,因为我需要2活动。 One for the TilesFragment and another for the DetailsFragment? 一个用于TilesFragment,另一个用于DetailsFragment? Currently I am trying to do this with a single activity? 目前,我正在尝试通过一个活动来执行此操作?

Current this is my code snippet for MainActivity (only showing the fragment transactions) 当前,这是我的MainActivity代码片段(仅显示片段事务)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");

        setContentView(R.layout.activity_main);

        /* Load ListFragment */
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);

        /* If the fragment is null then we need to load this fragment */
        if(fragment == null) {
            fragment = TitlesFragment.newInstance(1234);
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.add(R.id.fragment_container, fragment, "TitlesFragment");
            fragmentTransaction.commit();
        }
    }

    @Override
    public void onColorPlateSelected(long plateID) {
        Log.d(TAG, "onColorPlateSelected: " + plateID);
        /* Get the plate ID and pass it to the details fragment */

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        /* Replace the fragment with the new color plate */
        fragmentTransaction.replace(R.id.fragment_container, DetailsFragment.newInstance(plateID), "DetailsFragment");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fragmentTransaction.commit();
    }

My layout for activity_main: 我的activity_main布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

Here the ActivityMain will replace the fragment in the framelayout. 在这里,ActivityMain将替换帧布局中的片段。

I have seen this do by handling the OnBackPressed event handler. 我已经通过处理OnBackPressed事件处理程序看到了这一点。 But I am wondering if I can do it without that? 但是我想知道如果没有它我能做到吗?

Many thanks for any suggestions, 非常感谢您的任何建议,

Try just adding the same addToBackStack() method to your first list fragment that you add to your activity. 尝试仅将相同的addToBackStack()方法添加到您添加到活动中的第一个列表片段中。 ie

fragment = TitlesFragment.newInstance(1234);
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.add(R.id.fragment_container, fragment, "TitlesFragment");

            fragmentTransaction.addToBackStack(null);

            fragmentTransaction.commit();

This should then prevent the second fragment replacing it in the back stack. 然后,这应防止第二个片段在后堆栈中替换它。

I have managed to solve the issue. 我设法解决了这个问题。 My main activity's signature was this: 我主要活动的签名是这样的:

public class MainActivity extends ActionBarActivity  {
   ..
}

I was extending the MainActivity to inherit from the ActionBarActivity. 我正在扩展MainActivity以从ActionBarActivity继承。 However, when I changed the class signature to this: 但是,当我将类签名更改为:

public class MainActivity extends Activity  {
    ..
 }

Everything worked as expected and I was able to navigate to the titlesFragment from the detailsFragment using the backButton. 一切都按预期工作,我可以使用backButton从detailsFragment导航到titlesFragment。

Not sure why this is, maybe when using fragments the actionbar will be better off implemented in the fragments themselves and rather than in the main activity. 不知道为什么会这样,也许在使用片段时,最好在片段本身而不是在主要活动中实现操作栏。

Hope this can help someone else 希望这可以帮助别人

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

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