简体   繁体   English

对话框:在后台切换活动

[英]dialog: switch activity in background

Can I start an activity in the background of a dialog? 我可以在对话框的后台开始活动吗?

What I would like: Activity starts dialog, dialog swaps activity shown in the background. 我想要的是:“活动启动对话框”,对话框交换后台显示的活动。 The dialog is not dismissed. 对话框未关闭。

I think this can only be done when Dialog is also an Activity. 我认为这只能在Dialog也是Activity时才能完成。

What I thought was a solution: 我认为是一种解决方案:

  1. I send a localbroadcast from DialogActivity to Act 1 我从DialogActivity发送Act 1的本地广播
  2. Act 1 starts Act 2 and destroys itself. 行为1开始行为2,并自行毁灭。

Problem: Act 2 will be shown in front of DialogActivity. 问题:Act 2将显示在DialogActivity的前面。 I want it to switch at the back. 我希望它在后面切换。

Is this possible? 这可能吗?

Follow these couple of points to have a working solution: 请遵循以下几点,以找到可行的解决方案:

  1. To make the dialog's window appear on top of all the application windows, set its window type as system window. 要使对话框的窗口出现在所有应用程序窗口的顶部,请将其窗口类型设置为系统窗口。
  2. Use the application context to create the dialog. 使用应用程序上下文创建对话框。 If you use the activity context to create the dialog, the moment activity is finished, the dialog will also be terminated, as it is considered as window leakage by the system. 如果使用活动上下文创建对话框,则活动结束后,该对话框也将终止,因为该对话框被系统视为窗口泄漏。

Here is the sample code which demonstrates the working solution. 这是演示工作解决方案的示例代码。

// This is a button click handler.
public void launchDialog(final View v) {
    // Create the dialog with application context
    AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
            .setTitle("Title")
            .setMessage("This is dialog")
            .create();
    // Set the window type as system window
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialog.show();

    // Demonstrates the activity change behind the dialog.
    v.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(v.getContext(), MyActivity2.class);
            startActivity(intent);

            finish();
        }
    }, 3000);
}

Remember to set the permissions in manifest to use system windows: 切记在清单中设置权限以使用系统窗口:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

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

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