简体   繁体   English

将活动移到AsyncTask的最前面

[英]Move activity to front in AsyncTask

In an activity, I have created a AsyncTask after hiding the activity: 在一个活动中,我在隐藏活动之后创建了一个AsyncTask:

this.moveTaskToBack(true);
(new MyTask(this)).execute();

To show a dialog in the task (in onPostExcecute ), I want to bring the activity to front: 为了显示任务中的对话框(在onPostExcecute ),我想将活动放在最前面:

alertDialog.show();

Intent intent = new Intent(mainActivity, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.getBaseContext().startActivity(intent);

But a new instance of the main activity is created and shown on top of the dialog, although the application was still running (the activity has also a dialog style Theme.Dialog). 但是,尽管该应用程序仍在运行(该活动还具有对话框样式Theme.Dialog),但仍创建了一个主要活动的新实例并将其显示在对话框顶部。 How should I fix this? 我该如何解决?

Edit: According to javadoc , this code always recreates the activity and doesn't bring its previous instance to front, since startActivity is called from outside of an Activity Context. 编辑:根据javadoc ,此代码始终重新创建活动,并且不会将其先前的实例放在最前面,因为startActivity是从活动上下文外部调用的。

How about adding a new piece of information to that intent, and catching it in onCreate() ? 如何向该意图添加一条新信息,并在onCreate()捕获它?

What I mean is something like this: 我的意思是这样的:

public class MainActivity extends Activity {

    public static final String WANT_DIALOG_EXTRA = "WANT_DIALOG_EXTRA";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().hasExtra(WANT_DIALOG_EXTRA)) {
            // create and show dialog
        }
    }

}

Then when you create your intent, add one more line like this: 然后,当您创建意图时,再添加一行,如下所示:

intent.putExtra(MainActivity.WANT_DIALOG_EXTRA, true);

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

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