0

我目前有一组选项卡,每个选项卡都绑定到一个片段,我终于在其中一个选项卡上获得了 UI,以查看它应该如何。这是应用程序正确加载后的外观截图(在初始启动并滚动到选项卡之后):在此处输入图像描述现在,在我点击后退按钮之后(我没有也不会为每个选项卡使用 backStack -我不喜欢那种 UI 范例)然后使用应用程序切换器或从启动器重新启动,我的 questionRows 消失了(它们不会重新加载)。我需要在 onPause 中实现一些东西来保存我的显示吗?为什么 onResume 不处理这个?

package com.davekelley.polling;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.support.v4.app.Fragment;

public class EconFragment extends Fragment {

    private ViewGroup container;
    private TableLayout questionContainer;
    private ScrollView scrollView;
    private ViewGroup econFragment;
    static int pos = 0;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("Econ", "onCreateView");
        this.container = container;
        return inflater.inflate(R.layout.econfragment, container, false);
    }

    public void onResume() {
        super.onResume();
        questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer);

        LayoutInflater inflater = (LayoutInflater) getActivity().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //these lines are my first attempt to try and get the questions to repopulate after returning
        //from pressing back - as of yet, they don't repopulate the screen:
        //View econFragment = inflater.inflate(R.layout.econfragment, null);
        //container.addView(econFragment);

        int leftMargin=5;
        int topMargin=5;
        int rightMargin=5;
        int bottomMargin=5;
        while (pos < 10) {
        View question = inflater.inflate(R.layout.question, null);
        question.setId(pos);
        pos++;
        TableRow tr = (TableRow) question;
        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);
        questionContainer.addView(tr);
        }
        Log.d("Econ", "onResume");
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d("Econ", "onAttach");
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("Econ", "onCreate");

    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("Econ", "onActivityCreated");
    }

    public void onStart() {
        super.onStart();
        Log.d("Econ", "OnStart");
    }

    public void onPause() {
        super.onPause();    
        Log.d("Econ", "onpause");
    }

    public void onStop() {
        super.onStop();
        Log.d("Econ", "onstop");
    }

    public void onDestroyView() {
        super.onDestroyView();
        Log.d("Econ", "ondestroyview");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d("Econ", "ondestroy");

    }

    public void onDetach() {
        super.onDetach();
        Log.d("Econ", "ondetach");
    }
}
4

2 回答 2

1

每个 Activity 都有自己的生命周期,从onCreateonDestroy。在此处查看图片和非常详细的文章:活动生命周期

当用户按下PowerButtonBackButton时,您的活动会循环并自动终止。当您打开电源或重新启动时,活动将从开始(onCreate)开始。

您应该实现保存加载功能,并在onPaused回调和加载状态onResume回调上保存状态。它将防止在重新启动之间丢失您的活动状态。

于 2012-03-23T06:13:51.887 回答
0

当我在我的应用程序的其他领域工作时,这个问题有点被搁置了。事实证明,解决方案很简单:代码中列出的 pos 变量是静态的,因此 while 循环无法从 0-10 重新爬升。答案是这样的:即使 onCreateView 再次运行,View 在 BACK 按下后也没有正确重新组装?

于 2012-03-31T22:21:17.150 回答