简体   繁体   English

Android-finish()方法上的StartActivityForResult冻结UI

[英]Android - StartActivityForResult on finish() method freezes UI

I have the following situation: 我有以下情况:

  1. Activity A starts Activity B, by using startActivityForResult 活动A通过使用startActivityForResult启动活动B
  2. Activity B then returns an ArrayList of Strings to Activity A by using on finish() . 然后,活动B通过使用finish()将字符串的ArrayList返回给活动A。

Here is a code example of what exactly Activity B does: 这是活动B确切执行的代码示例:

ArrayList<String> urls = new ArrayList<>();
urls.add("Some string");

Intent intent = new Intent();
intent.putStringArrayListExtra(KEY, urls);
setResult(Activity.RESULT_OK, intent);
finish();

Then Activity A receive the data in onActivityResult(...) 然后活动A在onActivityResult(...)接收数据

The issue I have is that when the user taps the done button and Activity B's code example executes, Activity B freezes for about 3 seconds (when I have about 2 strings in the ArrayList ). 我的问题是,当用户点击完成按钮并且执行活动B的代码示例时,活动B冻结大约3秒钟(当ArrayList有大约2个字符串时)。 The more strings I have in the ArrayList the longer it freezes. 我在ArrayList拥有的字符串越多,冻结的时间就越长。 I have more or less determined that it is finish() that causes the UI thread to freeze. 我已经或多或少地确定是导致UI线程冻结的finish()

Is there a way to call finish() without freezing Activity B? 有没有一种方法可以finish()冻结活动B的情况下调用finish() If not, why is this happening? 如果没有,为什么会这样?

EDIT: Here is the full example: 编辑:这是完整的示例:

   /**
    * Background task
    */
   private class gatherUrlsTask extends AsyncTask<ArrayList<PictureEntry>, Integer, Intent> {

       @Override
       protected void onPreExecute() {
           progressBar.setVisibility(View.VISIBLE);
           bt_done.setVisibility(View.GONE);
           fab_add_picture.setVisibility(View.GONE);
       }

       @SafeVarargs
       @Override
       protected final Intent doInBackground(ArrayList<PictureEntry>... params) {
           ArrayList<String> imagePaths = new ArrayList<>();

           for (PictureEntry pictureEntry : params[0]) {
               if (pictureEntry.isSelected()) {
                   imagePaths.add(pictureEntry.getPath());
               }
           }

           if (imagePaths.size() == 0) {
               Toast.makeText(getApplicationContext(), R.string.please_select_atleast_one_image, Toast.LENGTH_LONG).show();
               return null;
           } else {
               Intent intent = new Intent();
               intent.putStringArrayListExtra(SELECTED_IMAGES_KEY, imagePaths);
               return intent;
           }
       }

       @Override
       protected void onPostExecute(Intent intent) {
           setResult(Activity.RESULT_OK, intent);
           finish();
       }
   }

However I can remove everything from the AsyncTask since it did not have any effect on performance. 但是,我可以从AsyncTask中删除所有内容,因为它对性能没有任何影响。

我不知道是什么原因造成的,但是为了防止ui冻结,请使用asyncTask,然后在onPostExcecute调用finish()

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

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