简体   繁体   English

Android Java:当对话框关闭时,如何防止我的对话框短暂显示 MainActivity appname?

[英]Android Java: How do I prevent my Dialog box from showing MainActivity appname briefly when the Dialog closes?

I'm fairly new to Android development and I've created my first "real" application that does the following:我对 Android 开发还很陌生,我创建了我的第一个“真实”应用程序,它执行以下操作:

  • Launches MainActivity启动 MainActivity
  • MainActivity processes Extra Data and then displays a ViewDialog that extends Dialog . MainActivity 处理额外数据,然后显示一个扩展DialogViewDialog ViewDialog has a showDialog() method that does the following to setup and display the Dialog : ViewDialog有一个showDialog()方法,它执行以下操作来设置和显示Dialog

     protected void showDialog(final Activity activity) { dialog = new Dialog(activity); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(false); dialog.setContentView(dialog_layout); // Set background color of the dialog ConstraintLayout currentLayout = (ConstraintLayout) dialog.findViewById(R.id.Dialog); // setup of views etc... // Finally dislay `Dialog` dialog.show(); // Method called to start a `DialogTimer` which extends `CountDownTimer` }
  • MainActivity shows the ViewDialog as follows: MainActivity 显示ViewDialog如下:

     public class MainActivity extends AppCompatActivity { private static Context appContext; private static ViewDialog notify; protected void onCreate(Bundle savedInstanceState) { // methods and processing etc... // time to display dialog notify = new ViewDialog(mParameters, mThemeHandler ); // ******************** Show dialog box ******************* notify.showDialog(activity: this); // showDialog just calls `Dialog.show()` notify.ApplyTheme(); }
  • When the timer expires or the user presses a button the ViewDialog is closed and the application is finished with the following code:当计时器到期或用户按下按钮时, ViewDialog将关闭,应用程序将使用以下代码完成:

     mButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CancelTimer(); activity.finishAndRemoveTask(); dialog.dismiss();

The problem is that when the ViewDialog is dismissed I can occasionally see what looks like a message that is displaying the activities android:label that is setup in the AndroidManifest file.问题是,当ViewDialog被关闭时,我偶尔会看到一条消息,显示在 AndroidManifest 文件中设置的活动android:label

Video of what is happening正在发生的事情的视频

I'm not sure why this happens, but I assume it's displaying some item of the MainActivity layout when the ViewDialog closes that uses it's own dialog_layout layout file.我不确定为什么会发生这种情况,但我假设它在ViewDialog关闭时显示MainActivity布局的某些项目,它使用它自己的dialog_layout布局文件。

I've fiddled with so many different things and changed code/layouts etc and I haven't been able to find my error.我摆弄了很多不同的东西并更改了代码/布局等,但我无法找到我的错误。

What are some pointers and hints that will help me fix this?有哪些指针和提示可以帮助我解决这个问题? I'm happy to provide more details if needed.如果需要,我很乐意提供更多详细信息。

The layout and manifest files are here:布局和清单文件在这里:

You can achieve this by setting the android:theme attribute to @android:style/Theme.NoTitleBar on your element in your AndroidManifest.xml like this:您可以通过在 AndroidManifest.xml 中的元素上将 android:theme 属性设置为 @android:style/Theme.NoTitleBar 来实现此目的,如下所示:

<activity android:name=".Activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The code you've posted is not enough to figure out why this is happening, but there is one universal solution not to see Activity title(that is is set in the label field of manifest).您发布的代码不足以弄清楚为什么会发生这种情况,但是有一个通用的解决方案看不到活动标题(即在清单的 label 字段中设置)。

Declare in your styles.xml a new theme like在您的 styles.xml 中声明一个新主题,例如

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowContentOverlay">@null</item>
</style>

and after that make it to be the theme of your activity like this然后让它成为你活动的主题,就像这样

 <activity
           ...
           android:name="boha.notify.MainActivity"
           android:theme="@style/AppTheme.NoActionBar">
           ...
</activity>

While I am not 100 percent sure(because this behavior can be altered in an actual code of your activity) I think it might help.虽然我不是 100% 确定(因为这种行为可以在您的活动的实际代码中改变),但我认为它可能会有所帮助。

I hope it will help.我希望它会有所帮助。

notify.setOnDissmissListener({CancelTimer()
    activity.finishAndRemoveTask()
    dialog.dismiss()})

It turns out that if I change:事实证明,如果我改变:

public class MainActivity extends AppCompatActivity {

to

public class MainActivity extends Activity {

the problem goes away.问题消失了。 I don't know why, but I'll take it.我不知道为什么,但我会接受它。

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

相关问题 如何检测用户何时关闭Android中的usd对话框? - How do I detect when user closes ussd dialog in Android? 我如何从 Android Studio 的 Mainactivity 中的 Dialog 做出反应? - How can i react from Dialog in Mainactivity in Android Studio? 当达到特定分数时,如何在我的骰子游戏中添加警报对话框? Java,Android工作室 - How do I add an Alert Dialog to my Dice Game when a certain score is reached? Java, Android Studio 如何使用 json object 从上下文处于 mainactivity 的对话框的 editText 发送数据? - how can i send data from editText of dialog box whose context is in mainactivity using json object? 如何通过对话框从我的网站打开 android 应用程序? - How to open an android app from my website with a dialog box? 对话框中未显示android loader - android loader not showing in dialog box Android对话框无法正确显示 - Android dialog box not showing properly 如何使用 java 将数据从自定义对话框传递到 android 工作室中的片段? - How can i pass data from custom dialog box to Fragment in android studio using java? 如何防止文本在警报对话框中换行? - How do I prevent the text from wrapping in a alert dialog? 如何在对话框的EditText视图中捕获信息? - How do I capture information from EditText view in a dialog box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM