2017-03-06 1 views
0

3 개의 (A, B, C) 조각과 하나의 activity.fragment A가 활동에 추가 된 다음 B, C가 바뀝니다. 이제 조각 B가 조각 Bin으로 대체됩니다. B i 화면을 잠금 해제 한 후 화면을 잠급니다. 조각이 A (추가됨) 인 열린 활동입니다. 화면을 잠그고 잠금 해제 한 후에 조각 B를 복원하는 방법화면 잠금 및 잠금 해제 후 조각 복원

답변

1

다음을 확장하는 클래스 내에 상태를 저장해야합니다. Application 클래스, 원인 활동은 디스플레이가 변경된 후에 해제됩니다 (화면 잠금 또는 방향 변경).

새 Application 클래스 :

public class myApp extends Application { 
    public int state; //field that keeps saved state 
활동 클래스 내부

:

//add this method to save changed state 
//then call it every time you change the fragment index 
private void onChangeFragment(int stateid) { 
    myApp sapp = (myApp) this.getApplication(); 
    sapp.state = stateid; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);        

    myApp sapp = (myApp) this.getApplication();    
    //restore fragment from sapp.state value 
    switch (sapp.state) { 
     case 0 : //fragment A 
      { setContentView(R.layout.fragmentA); 
       //maybe Fragment newFragment = new MyFragmentA(); ... and so on 
       break; 
      } 
     case 1 : //fragment B 
      { setContentView(R.layout.fragmentB); 
       //maybe Fragment newFragment = new MyFragmentB(); ... and so on 
       break; 
      } 
    } 

그리고 매니페스트 <application android:icon="@drawable/icon" android:label="@string/app_name" ... 안드로이드 내부 : 이름 = "을 myApp."`>

다른 방법은 활동의 이전에 저장된 상태를 Bundle savedInstanceState까지 사용하는 것입니다. 활동 클래스 내부

:

private int state; //field that keeps saved state 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    state = savedInstanceState.getInteger(FRAGMENT_STATE_KEY); 
    //restore the fragment from state value here 
    //switch (state) {.... 
    //.... 
} 

// invoked when the activity may be temporarily destroyed, save the instance state here 
@Override 
public void onSaveInstanceState(Bundle outState) { 
    out.putInteger(FRAGMENT_STATE_KEY, state); 

    // call superclass to save any view hierarchy 
    super.onSaveInstanceState(out); 
+0

당신은 코딩 breifly 설명시겠습니까? – Karthik

+0

코드에 무엇을 추가해야합니까? – user3811082

관련 문제