如果应用程序最小化的Andr​​oid活性片段生命周期的问题 [英] Android activity and fragment lifecycle issues in case of app minimization

查看:138
本文介绍了如果应用程序最小化的Andr​​oid活性片段生命周期的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多选择列表的片段。我想挽救那些在列表中当前检查的项目实例,并在情况下的应用程序最小化和这样的。

I have a fragment with a multi-choice list. I am trying to save instance of the items that are checked in the list currently and restore them in case of app minimization and such.

测试步骤:

  1. 到达的多选择列表中的片段。
  2. 检查几个列表项
  3. preSS Home键最小化的应用程序。
  4. preSS的多任务按钮,然后选择我的应用程序,以恢复它

下面是我使用的code:

Following is the code that i am using:

adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_multiple_choice,
        android.R.id.text1, listToShow);
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    mSelectedItems.clear();
                    int count = categoryList.getCheckedItemCount();

                    SparseBooleanArray booleanArray = categoryList.getCheckedItemPositions();

                    for (int i = 0; i < count; i++) {
                            mSelectedItems.add(booleanArray.keyAt(i));
                    }

                }
            });


@Override
public void onSaveInstanceState(Bundle outState) {
        Log.d("SubcategoryselectionList", "onsavedinstancestate");
        outState.putIntegerArrayList(SELECTED_ITEM, mSelectedItems);
        super.onSaveInstanceState(outState);
    }

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    Log.d("SubcategoryselectionList", "onViewStateRestored");
    if (savedInstanceState != null) {
        if (savedInstanceState.getIntegerArrayList(SELECTED_ITEM) != null) {
            Log.d("SubcategoryselectionList", "onViewStateREstored selected items found");
            mSelectedItems = savedInstanceState
                    .getIntegerArrayList(SELECTED_ITEM);
            for (int i = 0; i < mSelectedItems.size(); i++) {
                categoryList.setItemChecked(mSelectedItems.get(i), true);
            }
        }

    }
    super.onViewStateRestored(savedInstanceState);
}

而当我看到日志,该方法 onViewStateRestored 永远不会被调用。

为了解决这个问题我想用的活性生命周期和使用它的方法 onRestoreInstanceState()来达到上述效果,但令人惊讶的我不能看到,我加入到日志此方法。

To counter this I thought of using the activity lifecycle and use its method onRestoreInstanceState() to achieve the above mentioned effect but surprisingly I cant see the logs that I added to this method.

请人澄清在这种情况下,活动的生命周期和碎片如何工作的,我怎么能恢复在尽可能最好的方式我的状态有所帮助。

Please someone help in clarifying how the lifecycle of the Activity and Fragment working in this case and how can I restore my state in the best possible manner.

推荐答案

您应该实现的逻辑,你正在寻找的 onActivityCreated 你在哪里得到保存实例状态。

You should implement the logic of which you are looking in onActivityCreated where you get saved instance state.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

这篇关于如果应用程序最小化的Andr​​oid活性片段生命周期的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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