2016-09-02 4 views
1

저는 응용 프로그램을 작성 중입니다.
어딘가에 나는 활동의 조각을로드 할과 GRIDVIEW/목록보기 항목이 FirstFragment에 클릭 할 때, 나는로드 SecondFragment동일한 활동에서 조각 전환 동일한 조각을 다시 시작합니다.

내가 추가 한 조각 아래의 방법 :

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.form_creation_view); 

    Fragment fragment = new FirstFragment(); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.form_view, fragment).commit(); 

} 

두 번째 조각 :

Fragment fragment = new SecondFragment(); 
FragmentManager fragmentManager = getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.replace(R.id.form_view, fragment).commit(); 
MainActivity.java의

OnBackPress : SecondFragment이로드 될 때 나는 Mainactivity에서 BACK 버튼을 누르면

@Override 
public void onBackPressed() { 
    if(getFragmentManager().getBackStackEntryCount() == 0) { 
     super.onBackPressed(); 
    } else { 
     getFragmentManager().popBackStack(); 
    } 
} 

그래서, 그것은 FirstFragment로 전환,하지만 다시 FirstFragment의 onCreateView() 메소드를 호출합니다.

FirstFragment 내부에 Recyclerview가 있으며이 Recyclerview에 데이터를로드하기 위해 OnCreateView() 메소드의 API 요청을 서버로 전송합니다.

이제 다시 SecondFragment에서이 Fragment로 돌아 왔을 때 요청을 실행해서는 안되며 RecyclerView에 데이터를 채워야합니다.

어떻게하면됩니까?
아무도 도와 줄 수 있습니까?

+1

시작에이 목록을 저장하는 생각도한다. 그런 다음, 당신이'onBackPressed()'메쏘드에서 조각 상태를 관리하는 방법을 바꿀 것입니다. –

+0

당신이 할 수있는 한가지는 요청 시간을 저장하고 그것을 시작하기 전에 당신이 마지막으로했던 시간을 확인하는 것입니다. – lelloman

+1

하나의 간단한 방법 2ndFragment가 끝나는 동안 플래그가 설정되고 플래그 값이 변경되므로 FirstFragment의 onCreateView()가 실행될 때 플래그 값을 확인할 수 있습니다. – RRTW

답변

1

은 onCreateView에서,이 같은 검사를 수행해야합니다

if(mList.size() == 0){ 
     //make api call 
    }else{ 
     //set recyclerView adapter 
    } 

이 방법을, 당신이 RecyclerView를 다시 채울 수 있습니다 다시 SecondFragment에서 올 때.

당신은 당신의 활동의에서 onCreate 메서드에서`fragmentTransaction.replace` 대신`fragmentTransaction.add`를 사용하여 onSaveInstanceState

관련 문제