我遇到了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)
答案 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,您就失去了本地声明。
之前我遇到过类似的问题,但之后想出了解决方法,因此:
public class CustomActivity extends AppCompatActivity
MainAcvity.java and SecondActivity.java extend CustomActivity.java
最后你的文件看起来应该是这样的......
<强> 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();
}
});
}}
尝试这个让我们看看它是否有效(我祈祷它确实; - ))