0

현재 각 부분에 묶여있는 탭 세트가 있습니다. 탭 중 하나에서 UI를 가져와야합니다. 다음은 앱이 제대로로드 된 시점 (처음 실행 후 탭으로 스크롤 한 후)의 모습입니다. enter image description here 이제 뒤로 버튼을 클릭 한 후 (각 탭에 대해 backStack을 사용하지 않고 방해하지 않을 것입니다. - 나는 UI 패러다임을 좋아하지 않는다.) 그런 다음 앱 스위처를 사용하거나 런처에서 다시 시작하면 내 questionRows가 사라진다 (다시로드하지 않는다). 디스플레이를 저장하려면 onPause에 무엇인가를 구현해야합니까? 왜 onResume이이 문제를 처리하지 않습니까?BACK에서 앱으로 돌아간 후 인터페이스가 다시로드되지 않음

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

각 활동은 수명주기에서 onCreate에서 들의 OnDestroy에 자신있다. 여기에 사진을보고 매우 상세한 기사를 가지고 : Activities Lifecycle

귀하의 활동주기 불구하고 가서 사용자가 또는 BACK 버튼 PowerButton 을 누르면 자동으로 죽는다. 전원을 켜거나 다시 시작하면 활동이 시작될 때부터 시작됩니다 (onCreate).

당신은 부하 저장 및 기능을 구현하고 에 상태를 저장해야는 콜백 및 부하 상태 onResume 콜백을 onPaused. 재시작 사이에 활동 상태가 손실되는 것을 방지합니다.

+0

감사합니다, 당신은 내일 아침에 좀 더 깊이 제공 링크를 읽습니다. onPause에서 내 상태를 조금 저장하려고 이미 시작 했었습니다. 볼 수 있듯이 각 메서드에 대한 로그 항목이 있으므로 onPause 후에 내 물건을 잃어 버리고 있음을 알 수 있었기 때문에 그 이유에 대해 혼란 스럽습니다. UI는 다시 돌아올 때 단순히 재 작성되지 않습니다. 내 UI가 로그를 통해 볼 수있는 onResume에 내장되어 있기 때문에 반환 할 때 재실행합니다. putInt, putString 등의 메서드에 대해 자세히 살펴볼 것이지만이 형식 메서드를 통해 UI를 저장하는 방법을 파악하는 데 약간의 스트레칭이 될 것이라고 생각합니다. – Davek804

+0

기사를 실행 한 후 더 흡수해야한다고 생각합니다. 여기에서 문제를 알고 있다고 생각합니다. 프래그먼트에서 onCreateView는 onCreate (이 메소드에서 빌드 한 주 활동) 이후에 실행됩니다. 따라서 이전에 실행되는 메소드에서 레이아웃 (onCreateView에서 비정상적으로 증가)에 요소를 추가 할 수 없습니다. 어떤 아이디어? – Davek804

관련 문제