片段恢复:显示和隐藏事务不起作用

时间:2018-09-19 05:21:08

标签: android android-fragments kotlin

我想恢复后退栈中的android片段。在底部导航的不同选项卡之间切换时,我不希望重新创建片段视图。

我阅读了thisthisthis以及与此有关的关于堆栈溢出的其他一些问题。常见的建议是使用片段事务的显示和隐藏方法,但是它不起作用。这是我的kotlin代码:

SQL> CREATE TABLE assunto
  2  (
  3     ass_sigla       CHAR (3) CONSTRAINT pk_ass PRIMARY KEY,
  4     -- removed by LF assunto_ass_sigla_nn NOT NULL
  5     ass_descricao   VARCHAR2 (50) CONSTRAINT assunto_ass_descricao_nn NOT NULL
  6  );

Table created.

SQL>
SQL> CREATE TABLE editora
  2  (
  3     edi_codigo   NUMBER (1) CONSTRAINT pk_editora PRIMARY KEY,
  4     -- removed by LF editora_edi_codigo_nn NOT NULL
  5     edi_nome     VARCHAR2 (50) CONSTRAINT editora_edi_nome_nn NOT NULL
  6  );

Table created.

SQL>
SQL> CREATE TABLE autor
  2  (
  3     aut_matricula       NUMBER (6) CONSTRAINT pk_aut PRIMARY KEY,
  4     aut_nome            VARCHAR2 (50) CONSTRAINT autor_aut_nome NOT NULL,
  5     aut_cpf             NUMBER (11) CONSTRAINT autor_aut_cpf_nn NOT NULL,
  6     aut_dtnasc          DATE CONSTRAINT autor_aut_dtnasc_nn NOT NULL,
  7     aut_nacionalidade   VARCHAR2 (50)
  8                            CONSTRAINT autor_aut_nacionalidade_nn NOT NULL
  9  );

Table created.

SQL>
SQL> CREATE TABLE livro
  2  (
  3     liv_codigo       NUMBER (6) CONSTRAINT pk_liv PRIMARY KEY,
  4     -- removed by LF livro_liv_codigo_nn NOT NULL,
  5     liv_titulo       VARCHAR2 (50) CONSTRAINT livro_liv_titulo_nn NOT NULL,
  6     liv_preco        NUMBER (5, 2) CONSTRAINT livro_liv_preco_nn NOT NULL,
  7     liv_lancamento   DATE CONSTRAINT livro_liv_lancamento_nn NOT NULL,
  8     edi_codigo       NUMBER (6)
  9                         CONSTRAINT fk_liv_edi REFERENCES editora (edi_codigo)
 10                         NOT NULL,
 11     -- removed by LF livro_edi_codigo_nn NOT NULL
 12     ass_sigla        CHAR (3)
 13                         CONSTRAINT fk_liv_ass REFERENCES assunto (ass_sigla)
 14                         NOT NULL
 15  -- removed by LFlivro_ass_sigla_nn NOT NULL
 16  );

Table created.

SQL>
SQL> CREATE TABLE escreve
  2  (
  3     liv_codigo      NUMBER (6)
  4                        CONSTRAINT fk_esc_liv REFERENCES livro (liv_codigo),
  5     -- removed by LF NUMBER (1) CONSTRAINT  escreve_liv_codigo_nn NOT NULL,
  6     aut_matricula   NUMBER (6)
  7                        CONSTRAINT fk_esc_aut REFERENCES autor (aut_matricula),
  8     -- removed by LF NUMBER (1) CONSTRAINT escreve_aut_matricula_nn NOT NULL
  9     CONSTRAINT pk_esc PRIMARY KEY (liv_codigo, aut_matricula)
 10  );

Table created.

SQL>

总是显示backstack的最后一个片段。当我想隐藏它并显示 另一个片段,该片段屏幕变成白色,没有任何内容。

我的底部导航有四个项目,但是出于测试目的,我只使用了两个。片段中的两个是FirstFragment和SecondFragment。我正在使用v4.app.fragments。

1 个答案:

答案 0 :(得分:0)

With the help of Kishan Viramgama comment's, i solved my problem. I used "add" method instead of "replace". This is my code for resuming fragments: `

fun replaceFragment(fragment: Fragment) {
    var nameOfFragment: String? = fragment::class.simpleName
    var fragmentTransaction = supportFragmentManager.beginTransaction()
    var fm: FragmentManager = supportFragmentManager
    if (supportFragmentManager.findFragmentByTag(nameOfFragment) == null) {
        fragmentTransaction.add(R.id.root_fragments, fragment, nameOfFragment)      //if fragment is not added to the backstack,add it/ don't use replace because it creates new fragment emptying fragment container
        fragmentTransaction.addToBackStack(nameOfFragment)
        fragmentTransaction.commit()
    } else {
        var fragmentToShow:Fragment = supportFragmentManager.findFragmentByTag(nameOfFragment) //if fragment is already in backstack,show it and hide all other fragments.
        for (i in 0 until fm.backStackEntryCount) {
            var fragmentToHide: Fragment = fm.findFragmentByTag(fm.getBackStackEntryAt(i).name)
            if (fragmentToHide::class.simpleName != fragment::class.simpleName)
                fragmentTransaction.hide(fragmentToHide)
        }
        fragmentTransaction.show(fragmentToShow)
        fragmentTransaction.commit()
    }

}

`