English 中文(简体)
如何确定该活动不会现在暂停或被摧毁?
原标题:How to determine in onPostExecute that activity is not paused now or destroyed?

我开始 AsyncTask 并在任务构建器中输入 Context

onPostExecut 上,我想知道我的活动现在是否在活动,并且没有重建。如何更好呢?

为此,我可以:

  • onCreate 活动上创建随机数字,然后将其放入 Application 类;

  • 在Pause/onDestroy设置随机数为 0,在 onResume 中恢复随机数;

  • onPreecute () get 随机数字,在 on PostExecute 中,随机数字与应用程序随机数字比较。

我可以用背景来做决定吗?

最佳回答

有许多方法可以检查活动是否还在。

我通常会创建 Skeletonactivity , 其结构如下:

public class SkeletonActivity extends FragmentActivity {
    private boolean paused = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        paused = false;
    }
    @Override
    public void onPause() {
        super.onPause();
        paused = true;
    }
    @Override
    public void onResume() {
        super.onResume();
        paused = false;
    }
    @Override
    public void onStart() {
        super.onStart();
        paused = false;
    }
    public boolean isPaused() {
        return paused;
    }
}

现在让您的所有活动都扩展 Skeletonactivity 。 最后,您可以随意更改此底类以更改暂停的旗帜( 例如在 on Destroy () 中更新此标记)


另一种方法是在您的 Skeletonactivity 中设置上下文实例:

public class SkeletonActivity extends FragmentActivity {
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
    }
    @Override
    protected void onDestroy() {
        mContext = null;
        super.onDestroy();
    }
    public boolean isPaused() {
        return mContext==null;
    }
}
问题回答

请查看此示例 。 我确实将上下文保存在 AsyncTassk 中。 在“ 保留非配置” 中, 我设置它为无效 。 在“ 保存非配置” () 中, 我设置它为无效, 在“ 创建 () ” () 中, 我将上下文与当前运行中的活动相匹配 。 您可以在 Destroy () 中设置“ task = N” 来增强该功能 :

public class MyActivity extends Activity {

    /* package */ MyActivity context;

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

        public MyAsyncTask(final MyActivity context) {
            super();

            this.context = context;
        }

        @Override
        protected Cursor doInBackground(/* ... */) {
            // ...
        }

        @Override
        protected void onPostExecute(/* ... */) {
            if (context != null) {
                // ...
            }
        }

        @Override
        protected void onPreExecute (/* ... */) {
            // ...
        }
    }

    /* package */ MyAsyncTask task;

    @Override
    public void onCreate(final Bundle bundle) {
        // ...

        Bundle bundleExtras = getIntent().getExtras();
        if (bundleExtras != null) {
            task = (MyAsyncTask) getLastNonConfigurationInstance();
            if (task != null) {
                task.context = this;
                // ...
            } else {
                task = new MyAsyncTask(this);
                task.execute();
            }
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        if (task != null) {
            // ...

            task.context = null;
        }

        return task;
    }
}

它会帮助其他家伙满满。

如果您按住主按钮或 Stop () 上的后端按钮, 将会调用 。 所以声明活动内的一个变量, 如

   boolean isActivityclosed=false;

在 On Stop () 方法期间, 设定值为真实值 。

 @Override
    protected void onStop() {
        super.onStop();
        isActivityclosed=true;

    }

现在, 在 Excecute 方法后检查变量是否正确。 如果变量是真实的, 那么您的活动会在背景中 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签