2016-06-24 2 views
0

나는 실행중인 응용 프로그램이 있습니다. 사용자가 응용 프로그램으로 돌아갈 때 servise가 실행 중인지 확인하고 이것이 맞다면이 서비스에 연결하여 일부 데이터가있는 조각을 표시하려고 시도합니다. 데이터치명적인 예외 : java.lang.IllegalStateException : 활동이 파괴되었습니다.

private void callGPSFragment() { 
     ... 
     GPSServicePresenter gpsServicePresenter = new GPSServicePresenter(); 
     showGPSFragment(gpsServicePresenter); 

통화 조각

public void showGPSFragment(GPSServicePresenter gpsServicePresenter) { 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.replace(R.id.body_fragent, gpsServicePresenter); 
    Fragment oldFragment = fragmentManager.findFragmentByTag(SplashFragment.tag()); 
    if (oldFragment != null) { 
     transaction.remove(oldFragment); 
    } 
    transaction.commitAllowingStateLoss(); 
} 
을 조각을 만들고 채우기 - 연결이 완료되면 servise

private void connectService() { 
    registerFitnessReceiver(); 
    sConn = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      if (service != null) { 
       callGPSFragment(status, mapWaySelections, activityType); 
      } 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 

     } 
    }; 
    Intent requestIntent = new Intent(MainActivity.this, FitnessRecordingService.class); 
    requestIntent.setAction(FitnessRecordingService.GET_FITNESS_RECORD_ACTION); 
    bindService(requestIntent, sConn, 0); 
} 

와 연결

Fatal Exception: java.lang.IllegalStateException: Activity has been destroyed 
     at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1433) 
     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687) 
     at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:667) 
     at com.fjuul.fjuul.MainActivity.showGPSFragment(MainActivity.java:712) 

시도 :하지만 가끔은 이런 실수를 얻을

+0

줄 번호는 '712'입니까? –

+1

@ ρяσѕρєяK 예외를 기반으로 마지막으로 추측됩니다 : 'transaction.commitAllowingStateLoss(); ' – REG1

+0

예. 사실입니다. commitAllowingStateLoss() 이후에 예외가 표시됩니다. – Devnock

답변

2

문제는 서비스에 대한 연결이 비동기 적으로 발생하고 활동이 소멸 된 후 onServiceConnected(ComponentName, IBinder)이 호출 될 수 있습니다. 이 파괴 된 것 후

이 문제를 해결하기 위해, 당신은 당신의 활동 상태를 확인하고 상호 작용을 방지하기 위해 (그리고/또는 isFinishing() 당신이 안드로이드의 이전 버전을 지원하려는 경우) Activity.isDestroyed() 기능을 사용할 수

@Override 
public void onServiceConnected(ComponentName name, IBinder service) { 
    if (service != null && !isDestroyed()) { 
     callGPSFragment(status, mapWaySelections, activityType); 
    } 
} 
+0

감사합니다. 친구. 나는 그것에 대해 생각했지만이 단편을 보여줄 필요가있다. 이 경우 어떻게 할 생각인가요? 나도 몰라, Activiti 또는 다른 것을 다시 만들 수 있을까? – Devnock

+0

유스 케이스에 따라 다릅니다. 사용자가 그것을 버렸기 때문에 활동이 파괴 된 경우 (예 : 뒤로 버튼 누르기) 아무 것도 표시하고 싶지 않을 것입니다. 액티비티가 여전히 관련이 있다면'startActivity()'를 다시 호출하는 것이 좋습니다. –

+0

hm. 서비스의 일부 데이터를 의도에 따라 보냅니다. 큰! 나는 이것을 시험해 본다. – Devnock

관련 문제