重新启动活动后,如何还原活动的片段? [英] How do fragments of an activity get restored when the activity restarts?

查看:84
本文介绍了重新启动活动后,如何还原活动的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试以下情况:由于内存不足,系统终止了应用程序进程后,用户进入了我的应用程序.我看到了意外的行为,并希望得到一些帮助.

I am testing situation where a user goes into my app after the system has terminated the app process due to low RAM. I am seeing unexpected behavior and hoping to get some help.

在我的应用程序中,我有一个活动,将其称为ActivityA,它会立即创建一个片段Fragment A,并进行片段替换. FragmentA显示包含两个项目的ListView.如果用户单击第一项,则会创建第二个片段Fragment B并替换FragmentA.否则,将创建另一个FragmentA并替换原始的FragmentA.我正在尝试创建文件目录树. FragmentA用于目录,FragmentB用于文件.

In my app, I have an activity, lets call it ActivityA, that immediately creates a fragment, Fragment A, and does a fragment replacement. FragmentA displays a ListView with two items in it. If the user clicks the first item, a second fragment, Fragment B is created and replaces FragmentA. Otherwise, another FragmentA is created and replaces the original FragmentA. I'm trying to create a file directory tree. FragmentA is for directories, and FragmentB is for files.

可以说用户单击了文件.这是测试中用户切换到另一个应用程序的阶段,由于内存不足,系统终止了我的应用程序进程.然后,用户返回我的应用程序,期望一切都保持原样.但是,实际发生的是正在显示Fragment A(父目录)而不是Fragment B(文件).当用户单击后退"按钮时,将显示Fragment B(文件).我做错了什么导致系统以这种方式还原Backstack?

Lets say the user clicks on a file. This is the stage in the test where the user switches to another app, and the system terminates my app's process due to low memory. Then, the user goes back into my app expecting everything to be left the way it was. However, what actually happens is Fragment A (The parent directory) is being displayed instead of Fragment B (the file). When the user clicks the back button, Fragment B (the file) is then displayed. What am I doing wrong that is causing the system to restore the backstack in this way?

这是一个示例程序,可以进一步显示我的应用程序在做什么:

Here is an example program to further show what my app is doing:

// ActivityA.java
public class ActivityA extends AppCompatActivity implements onItemClickListener
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        FragmentA fragA = new FragmentA();
        FragmentTransaction fragmentTransaction = 
            getSupportFragmentManager().beginTransaction();
        fragmentTransation.replace(R.id.basic_frame, fragA);
        fragmentTransaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> aView, View v, int position, long id)
    {
        if (position == 0)
        {
            FragmentB fragB = new FragmentB();
            FragmentTransaction fragmentTransaction = 
                getSupportFragmentManager().beginTransaction();
            fragmentTransation.replace(R.id.basic_frame, fragB);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
        else
        {
            FragmentB fragA = new FragmentA();
            FragmentTransaction fragmentTransaction = 
                getSupportFragmentManager().beginTransaction();
            fragmentTransation.replace(R.id.basic_frame, fragA);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    }
}

推荐答案

调用super.onCreate()时,当savedInstanceState不为null时,片段会自动恢复其当前状态.

When you call super.onCreate(), Fragments automatically restore their current state when savedInstanceState is not null.

因此,如果您希望通过添加初始片段来进行一次设置,则应始终使用if (savedInstanceState == null)检查将其包围:

Therefore if you're expecting to do one time set up by adding your intial Fragment, you should always surround it with an if (savedInstanceState == null) check:

@Override
public void onCreate(Bundle savedInstanceState)
{
    // I assume you accidentally left out these lines
    super.onCreate(savedInstanceState);
    setContentView(R.id.your_content_view);

    if (savedInstanceState == null) {
        FragmentA fragA = new FragmentA();
        FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction();
        fragmentTransation.replace(R.id.basic_frame, fragA);
        fragmentTransaction.commit();
    }
}

这篇关于重新启动活动后,如何还原活动的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆