0

現在、それぞれがフラグメントに関連付けられた一連のタブがあり、最終的にタブの 1 つで UI がどのように見えるかを確認しました。これは、アプリが適切にロードされたときのスクリーンショットです (最初の起動とタブへのスクロールのここに画像の説明を入力後)。私はそのUIパラダイムが好きではありません)、アプリスイッチャーを使用するか、ランチャーから再起動すると、質問行がなくなります(再読み込みしません)。ディスプレイを保存するには、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

各アクティビティには、onCreateからonDestroyまでの独自のライフタイム サイクルがあります。写真と非常に詳細な記事をご覧ください:活動のライフサイクル

ユーザーがPowerButtonまたはBackButtonを押すと、アクティビティは循環し、自動的に終了します。電源を入れるか再起動すると、begin ( onCreate ) からアクティビティが開始されます。

保存関数と読み込み関数を実装し、onPaused コールバックで状態を保存し、 onResumeコールバックで状態を読み込む必要があります。再起動の間にアクティビティの状態が失われるのを防ぎます。

于 2012-03-23T06:13:51.887 に答える
0

アプリの他の領域に取り組んでいたので、この質問は後回しにされてしまいました。コードにリストされている pos 変数は静的であったため、while ループは 0 から 10 に戻りませんでした。答えは次のとおりです: onCreateView が再度実行されても、BACK キーを押した後に View が適切に再構築されませんか?

于 2012-03-31T22:21:17.150 に答える