当Dialog片段被解除时,无法在onSaveInstanceState之后执行此操作

时间:2017-03-09 20:36:15

标签: android android-fragments android-dialogfragment

我遇到了DialogFragments的问题。我有一个Activity,它有一个按钮,onclick显示一个进度微调器(带自定义视图的DialogFragment),然后转到另一个活动。在我的第二个Activity中,我有button2,onclick将结果发送回我之前的活动,该活动应该关闭进度微调器。

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)

2 个答案:

答案 0 :(得分:1)

在这种情况下,DialogFragment在后​​台被解散,因此,只有通过dismissAllowingStateLoss()方法完成后,才允许解散。但是要使用此方法,您只需要使用supportFragmentManager来显示此对话框,就像这样:(对不起,答案在Kotlin中)

gameLoadingDialog = GameLoadingDialog()
gameLoadingDialog?.show(supportFragmentManager, "gameLoadingDialog") 

然后甚至可以在后台将其消除:

val prev = supportFragmentManager.findFragmentByTag("gameLoadingDialog")
if (prev != null && prev is GameLoadingDialog) {
    prev.dismissAllowingStateLoss()
}

答案 1 :(得分: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() dismissProgressDialog()移动到 CustomActivity.java 中,然后在MainActivity.java或SecondActivity中调用它们.java(因为这两种方法都将成为继承CustomActivity的任何类的共享公共方法)
  4. 最后你的文件看起来应该是这样的......

    <强> 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();
            }
        });
    }}
    

    尝试这个让我们看看它是否有效(我祈祷它确实; - ))