Interface not reloaded after returning to app from BACK press

走远了吗. 提交于 2019-12-25 08:47:54

问题


I currently have a set of tabs, each tied to a fragment, and I've finally gotten the UI on one of the tabs to look how it should. Here's a screenshot of how the app looks when it has properly loaded (after initial launch and scrolling to a tab):

Now, after I hit the back button (I haven't and won't be bothering with a backStack for each tab - I don't like that UI paradigm) and then use app switcher or relaunch from the launcher, my questionRows are gone (they don't reload). Do I need to implement something in onPause to save my display? Why doesn't onResume take care of it this?
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");
    }
}

回答1:


Each Activity has own lifetime cycle from onCreate to onDestroy. Take a look at picture and very detailed article here: Activities Lifecycle

Your activity go though cycles and die automatically when user pressed PowerButton or BackButton. When you turn power or relaunch then activity starts from begin (onCreate).

You should implement save and load functions and save state on onPaused callback and load state onResume callback. It will prevent from loosing state of your activity between restarts.




回答2:


The question kinda got put on the backburner as I worked on other areas of my app. It turns out the solution was a simple one: the pos variable as listed in the code was static, so the while loop could not reclimb from 0-10. Here's the answer: Even though onCreateView runs again, View does not properly reassemble after BACK press?



来源:https://stackoverflow.com/questions/9830123/interface-not-reloaded-after-returning-to-app-from-back-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!