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)
謝謝回答我的問題。我做了你要求我做的改變,但仍然遇到同樣的問題。 – user1401232
我發現一個更有趣的事情如果我圍繞我的解僱代碼與try/catch塊它的工作正常。 – user1401232
哦,那太棒了..豎起大拇指! – Nelis