简体   繁体   English

从对话框活动返回到MainActivity

[英]return from Dialog Activity to MainActivity

I have this issue I start an Activity as a Dialog with attribute android:theme="@android:style/Theme.Dialog" so far so good, when I run this Activity I want the user to fill some EditText s and then press a button where a background process will be started, now I use this.setFinishOnTouchOutside(false); 我有这个问题,到目前为止,我以属性android:theme="@android:style/Theme.Dialog"作为对话框启动一个Activity,当我运行此Activity时,我希望用户填充一些EditText ,然后按按钮,将在其中启动后台进程,现在我使用this.setFinishOnTouchOutside(false); to pervent the user from clicking outside and finish() being called, my problem is that I don't get how to finish() specifically this DialogLikeActivity, when i call finish() after the background process is started the application is close and i want to return to the MainActivity or the Activity that started the DialogLikeActivity (the MainActivity still visible after the startActivity or startActivityForResult() is called for the DialogLikeActivity, here is what i got: 为了防止用户单击外部并调用finish() ,我的问题是我不知道如何特别地finish()此DialogLikeActivity的finish() ,当我在后台进程启动后调用finish()时,应用程序关闭并且我想返回到MainActivity或启动DialogLikeActivity的活动(在为DialogLikeActivity调用startActivitystartActivityForResult()之后,MainActivity仍然可见startActivityForResult()

Code From MainActivity: 来自MainActivity的代码:

Intent intent = new Intent(this, DialogLikeActivity.class);
        startActivityForResult(intent,0);

and the button code in DialogLikeActivity: 以及DialogLikeActivity中的按钮代码:

public void saveClient(View view){
    Intent returnIntent = new Intent();
    setResult(RESULT_OK, returnIntent);
    finish();


}

I guess maybe my problem is that MainActivity is not a parent of DialogLikeActivity, in that case it should be? 我想也许我的问题是MainActivity不是DialogLikeActivity的父级,在这种情况下应该是吗? how to make DialogLikeActivity child of MainActivity and if I achieve that would the call for finish() in DialogLikeActivity finish the Activity itself and not the app? 如何使MainActivity的DialogLikeActivity成为子级,如果实现了该要求,则DialogLikeActivity中对finish()的调用会完成Activity本身而不是应用程序吗?

Thanks in advance and sorry for my english. 在此先感谢您,我的英语很抱歉。

EDIT: 编辑:

this is the Activity being displayed as a Dialog above the MainActivity 这是活动在MainActivity上方显示为对话框

It sounds like DialogFragment might better suit your needs here. 听起来DialogFragment可能更适合您的需求。 It has lifecycle methods similar to an Activity and can run background tasks within itself, but is actually managed by the activity it is attached to. 它具有类似于“ Activity生命周期方法,并且可以在其内部运行后台任务,但实际上是由与其关联的活动来管理的。 There are several types you can use. 您可以使用几种类型。

Check out the Google documentation on it HERE . 此处查看Google文档。

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

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