简体   繁体   English

Android:在Activity恢复后更改DialogFragments的顺序

[英]Android : Order of DialogFragments changing after Activity is resumed

In my activity, two DialogFragments are open. 在我的活动中,两个DialogFragments已打开。

Dialog1 and then Dialog2 above the Dialog1 (Both can be seen, one overlapping the other). Dialog1然后在Dialog1上方的Dialog2(两者都可以看到,一个与另一个重叠)。

I open some other activity, and when I come back to my original activity through task manager, the order of the dialogs has changed..ie I now see Dialog1 over the Dialog2. 我打开了一些其他活动,当我通过任务管理器回到原来的活动时,对话框的顺序发生了变化..我现在看到Dialog2上的Dialog1。

How can I maintain the order of the DialogFragments when I resume my activity from the TaskManager?? 当我从TaskManager恢复活动时,如何维护DialogFragments的顺序?

I had similar problem with the ordering of DialogFragments. 我对DialogFragments的排序有类似的问题。 In my case i had a dialog to set a time period and therefore it had to start also another dialog for picking dates. 在我的情况下,我有一个对话框来设置一个时间段,因此它必须启动另一个选择日期的对话框。 So after showing the second dialog and change the orientation of the screen, the order of two dialogs reversed, similar to your problem. 因此,在显示第二个对话框并更改屏幕方向后,两个对话框的顺序相反,类似于您的问题。

So, my solution was to create an FragmentActivity that looked like a dialog. 所以,我的解决方案是创建一个看起来像对话框的FragmentActivity。 I replaced it then with the first dialog. 我用第一个对话框替换了它。 That way i could better manage saving instance state and stuff like that with Activity's lifecycle. 这样我就可以更好地管理保存实例状态以及与Activity的生命周期类似的东西。 This activity called then as usual the second dialog, without carrying something else. 这个活动像往常一样调用第二个对话框,而不带其他东西。

My other Activity, that actually called the previous (first) dialog had then just to call the new Activity for result and that was it! 我的另一个Activity,实际上调用了前一个(第一个)对话框,然后只是调用新的Activity for result,就是这样!

Here in more detail, how to make an activity to look like an dialog: 在这里更详细地说,如何使活动看起来像一个对话框:

class MyDialogActivity extends FragmentActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_dialog_layout);

    LayoutParams params = getWindow().getAttributes();
    params.height = LayoutParams.WRAP_CONTENT;
    params.width = LayoutParams.WRAP_CONTENT;
    getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    setTitle("my dialogs title");
    setTheme(DialogFragment.STYLE_NORMAL);
    // ...
}

I hope, it will help you, or at least give you an idea for a workaround for that ugly android bug! 我希望,它会帮助你,或者至少给你一个关于那个丑陋的android bug的解决方法的想法! Regards. 问候。

I had a similar trouble. 我遇到了类似的麻烦。 I created the first entity of DialogFragment. 我创建了DialogFragment的第一个实体。 Then I created the second one over the first. 然后我在第一个创建了第二个。 The problem appeared when I rotated my phone. 旋转手机时出现问题。 In this case the first DialogFragment relocated over the second. 在这种情况下,第一个DialogFragment重新定位在第二个。

It seems the issue is I created the first DialogFragment at the same place(method) where I dismiss another DialogFragment. 看来问题是我在同一个地方(方法)创建了第一个DialogFragment,在那里我解除了另一个DialogFragment。

I did a sort of: 我做了一点:

FirstDialogFragment a=FirstDialogFragment.newInstance(...);
a.show(getSupportFragmentManager(),"");
anotherDialogFragment.dismiss();
    .....
// then somewhere in another method
SecondDialogFragment b=SecondDialogFragment.newInstance(...);
b.show(getSupportFragmentManager(),"");

The issue was solved when I placed "anotherDialogFragment.dismiss()" before "FirstDialogFragment.newInstance()". 当我在“FirstDialogFragment.newInstance()”之前放置“anotherDialogFragment.dismiss()”时,问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM