2014-10-28 2 views
0

태그가있는 백 스택에 중첩 된 조각이있는 탐색 서랍이있는 곳에서이 시나리오를 구현했습니다. 모든 조각이 구현 된 다음했습니다 브로드 캐스트 수신기 및 조각

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (rootView == null) { 
     rootView = inflater.inflate(R.layout.fragment_feed, container, 
       false); 
     // Doing initialisaion here 
    } else { 
     ((ViewGroup) rootView.getParent()).removeView(rootView); 
    } 
    return rootView; 

내가 (이미 여기에 새로운 접근 방식을 구현하는 방법에 대한 생각)이 조각 안에 내 목록과 동적 머리글과 바닥 글을 유지하기 위해 생각할 수있는 최선의 방법이었다. 나는이 목록을 새로 고치거나 있도록 사용자가 내 응용 프로그램에서 LOGES 때

는 또한 나는이 방송을 때 위치 변경 :

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
      locationBroadcastReciever, 
      new IntentFilter(UpdateLocationIntentService.LOCATION_CHANGED)); 
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
      significantBroadcastReciever, 
      new IntentFilter(UserDefaultsManager.SIGNIFICANT_CHANGED)); 
    // Further code here 
} 

조각이 분리 될 때 분리에 호출된다

@Override 
public void onDetach() { 
    super.onDetach(); 
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(
      locationBroadcastReciever); 
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(
      significantBroadcastReciever); 
    // This is also where I cancel any pending request for fetching data 
}; 

이 방법은 현재 작동하지만 조각을 바꿀 때 onDetach가 호출되지 않기 때문에 onDetach가 호출되지 않았기 때문에 onDetroyView가 호출 되었기 때문에 onDestroyView가 호출되기 때문에 요청이 완료되었는지 확인해야합니다. 백 스택 및 i 아직 분리되지 않았습니다.

nullpointer 검사가있는 현재 구현은 사용자가 로그 아웃 (또는 로그인)되거나 위치가 변경되고 이전에 목록에 항목이없는 경우 동시에 4 개 조각을 업데이트 할 수있게합니다.

나는 더 좋은 방법이있을 것이라고 확신하지만, 나는 그 어느 때보다도 똑같은 결과를 얻는 것처럼 보일 수 없다.

+0

EventBus – cYrixmorten

+0

을 살펴 보거나 이벤트 버스 대신 Otto를 사용해 볼 수있는 것처럼 보입니다. –

+1

제안 해 주셔서 감사합니다. 나는 우리가 옳은 길에 있다고 생각하지 않습니다. 이벤트는 내 조각을 통해 전달됩니다 (문제는 아닙니다). 조각을 업데이트해야하고 onResume이 호출 될 때마다 플래그를 설정하여 더 나은 해결 방법을 통해이 새로 고침을 수행합니다 (이 방법으로 내 뷰가 null임을 염려 할 필요가 없습니다). –

답변

1

나의 경우 내가 찾은 MainActivity.The 간단한 해결책 주최 조각 중 하나를 사용하여 텍스트 필드를 업데이트하는 것이 었습니다이 있었다 : -

내 MainActivity 클래스에서

는 MainActivtiy.This의 실행중인 인스턴스 내 MAinActivity

입니다 검색
private static MainActivity mainActivityRunningInstance; 
    public static MainActivity getInstace(){ 
     return mainActivityRunningInstance; 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mainActivityRunningInstance =this; 
---------- 
} 

지금 브로드 캐스트 리시버의 onRecieve 방법에서 파를 위해 지금 업데이트 방법

@Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().matches(Intents.PROVIDER_CHANGED_ACTION)) { 
       String textValue="the text field value"; 
// the null check is for, if the activity is not running or in paused state, in my case the update was required onlyif the main activity is active or paused 
      if(MainActivity.getInstace()!=null) 
       MainActivity.getInstace().updateUI(textValue); 
} 

을 해당 인스턴스를 얻을 호출 UIThread에서 업데이트를 실행해야하는 위치, MainActivity의 updateUI 메서드는 조각 업데이트 메서드를 호출합니다.

public void updateUI(final String str) { 
     MainActivity.this.runOnUiThread(new Runnable() { 
      public void run() { 
    //use findFragmentById for fragments defined in XML ((SimpleFragment)getSupportFragmentManager().findFragmentByTag(fragmentTag)).updateUI(str); 
      } 
     }); 
    } 

최종 단계는 조각의 텍스트 필드

public void updateUI(String str){ 
     tv_content.setText(str); 
    } 

및 빙고를 업데이트하고는 그 다. 내 문제를 해결하기 위해 post을 언급했습니다. 다른 사람들을 돕기를 바랍니다.

관련 문제