2017-03-09 50 views
0

我遇到了DialogFragments問題。我有一個Activity,它有一個onclick按鈕,顯示一個進度微調器(帶有自定義視圖的DialogFragment),然後轉到另一個活動。在我的第二個活動中,我有button2,onclick會將結果發送回我之前的活動,該活動應該取消進度微調器。解除對話框片段時,onSaveInstanceState後無法執行此操作

public class MainActivity extends AppCompatActivity { 

private Button button; 
private int stackLevel; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    stackLevel=0; 

    button = (Button)findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showProgressDialog(); 

      Intent intent = new Intent(MainActivity.this,   SecondActivity.class); 
      startActivityForResult(intent,2); 
     } 
    }); 


} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(requestCode==2){ 

     dismissProgressDialog(); 

    } 

} 

public void showProgressDialog() { 
    stackLevel++; 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    Fragment prev = getSupportFragmentManager().findFragmentByTag("progressDialog"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    //ft.addToBackStack(null); 
    // Create and show the dialog. 
    DialogFragment newFragment = LoaderFragment.newInstance(stackLevel); 
    //newFragment.show(ft,"progressDialog"); 
    ft.add(newFragment, "progressDialog"); 
    ft.commitAllowingStateLoss(); 
} 


public void dismissProgressDialog() { 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    DialogFragment prev = (DialogFragment) getSupportFragmentManager().findFragmentByTag("progressDialog"); 
    if (prev != null) { 
     prev.dismiss(); 
    } 
     ft.commitAllowingStateLoss(); 
} 


} 

活動2

public class SecondActivity extends AppCompatActivity { 

private Button button; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second_layout); 

    button = (Button)findViewById(R.id.button2); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      try { 
       setResult(2); 
      }catch (Exception e){ 

       e.printStackTrace(); 
      } 


      finish(); 

     } 
    }); 

} 
} 

但我得到當我關閉該對話框片段

Caused by: java.lang.IllegalStateException: Can not perform this action  after onSaveInstanceState 
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1527) 
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1545) 
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654) 
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621) 
at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:201) 
at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:167) 
at com.example.am46517.samplefragmentapplication.MainActivity.dismissProgressDialog(MainActivity.java:70) 
at com.example.am46517.samplefragmentapplication.MainActivity.onActivityResult(MainActivity.java:42) 
at android.app.Activity.dispatchActivityResult(Activity.java:6926) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:4293) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)  
at android.app.ActivityThread.-wrap20(ActivityThread.java) 

回答

0

我覺得有下面的線有問題下面的錯誤; 「無法解析符號LoaderFragment」。

DialogFragment newFragment = LoaderFragment.newInstance(stackLevel); 

但你可能有你的代碼中的問題可能是你如何實例的ProgressDialog的原因。因爲一旦你離開MainActivity,你已經失去了本地聲明。

我以前也遇到過類似的問題,但後來想通變通的辦法,即:

  1. 創建一個父類的活動......
    public class CustomActivity extends AppCompatActivity
  2. MainAcvity.java and SecondActivity.java extend CustomActivity.java
  3. 移動這兩個對話框方法showProgressDialog () and dismissProgressDialog() into CustomActivity.java,然後在MainActivity.java或S中調用它們econdActivity.java(因爲這兩種方法會成爲共享的公共方法,任何類繼承CustomActivity)

到底你的文件應該是這個樣子,這些..

1 CustomActivity.java

public class CustomActivity extends AppCompatActivity { 
private int stackLevel; 
public void showProgressDialog() 
{ 
    stackLevel++; 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    Fragment prev = getSupportFragmentManager().findFragmentByTag("progressDialog"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    //ft.addToBackStack(null); 
    // Create and show the dialog. 
    DialogFragment newFragment = LoaderFragment.newInstance(stackLevel); 
    //newFragment.show(ft,"progressDialog"); 
    ft.add(newFragment, "progressDialog"); 
    ft.commitAllowingStateLoss(); 
} 


public void dismissProgressDialog() 
{ 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    DialogFragment prev = (DialogFragment) getSupportFragmentManager().findFragmentByTag("progressDialog"); 
    if (prev != null) { 
     prev.dismiss(); 
    } 
    ft.commitAllowingStateLoss(); 
}} 

2. MainActivity.java

public class MainActivity extends CustomActivity { 
@Override 
protected void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick (View view) 
     { 
      showProgressDialog(); 

      Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
      startActivityForResult(intent, 2); 
     } 
    }); 
} 

@Override 
protected void onActivityResult (int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == 2) { 
     dismissProgressDialog(); 
    } 
}} 

3 SecondActivity.java

public class SecondActivity extends CustomActivity { 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second_layout); 

    Button button = (Button) findViewById(R.id.button2); 
    button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) { 

      try { 
       setResult(2); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 

      finish(); 
     } 
    }); 
}} 

試試這個,讓我們看看它是否工作(我祈禱它;-))

+0

謝謝回答我的問題。我做了你要求我做的改變,但仍然遇到同樣的問題。 – user1401232

+0

我發現一個更有趣的事情如果我圍繞我的解僱代碼與try/catch塊它的工作正常。 – user1401232

+0

哦,那太棒了..豎起大拇指! – Nelis

相關問題