活动启动中的片段花费比预期更多的时间

时间:2018-12-05 18:38:31

标签: java android android-fragments android-fragmentactivity

我是初学者中级的android开发人员。我开始在此应用程序中处理片段。该应用程序的结构为:

  1. 其中一些重要链接嵌入按钮的主要活动
  2. 在主活动中单击按钮时,一些数据库任务已完成(已使用Room Library,因此所有任务都使用AsyncTask),并且新活动随意向附加链接一起打开
  3. 获得链接后,新的activity-2(本身)会添加一个片段,并在后台执行一些数据库任务,然后在片段的webview中打开链接。

问题是,从1-2开始仅花费0.1-0.15秒,而开始执行任务3,则花费0.3-0.45秒,因此从主活动中单击时,用户正在片段中打开链接(具有网络视图),大约需要0.6秒的时间,这让您感觉好像应用被冻结了。

以下是一些代码: 活动2:

@Override
protected void onCreate(Bundle savedInstanceState) {
    prefSingleton = PrefSingleton.getInstance();
    if (prefSingleton.getStorage().getBoolean(Constants.STORAGE_ENABLE_NIGHTMODE,false)){
        setTheme(R.style.DarkTheme);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_website_view);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowTitleEnabled(false);

    closeWebview = toolbar.findViewById(R.id.closeWebview);
    downArrow = toolbar.findViewById(R.id.downArrow);
    allTabs = toolbar.findViewById(R.id.allTabs);
    searchIcon = toolbar.findViewById(R.id.search);
    clearEditText = toolbar.findViewById(R.id.clearEditText);
    selectedSEImage = toolbar.findViewById(R.id.selectedSEImage);
    searchEditText = toolbar.findViewById(R.id.searchEditText);
    searchEngineFrame = toolbar.findViewById(R.id.searchEngine);
    searchBarLayout = toolbar.findViewById(R.id.searchBarLayout);
    toolbarTitle = toolbar.findViewById(R.id.toolBarTitle);
    navFrame = findViewById(R.id.navFrame);
    navRecView = findViewById(R.id.navRecView);

    if (prefSingleton.getStorage().getBoolean(Constants.STORAGE_ENABLE_NIGHTMODE,false)){
        navFrame.setBackgroundColor(getResources().getColor(R.color.night_mode_toolbar));
    }

    //Activity toolbar views
    closeWebview.setOnClickListener(this);
    closeWebview.setOnLongClickListener(this);
    downArrow.setOnClickListener(this);
    allTabs.setOnClickListener(this);
    searchIcon.setOnClickListener(this);
    clearEditText.setOnClickListener(this);
    searchEngineFrame.setOnClickListener(this);
    searchEditText.setOnClickListener(this);
    searchEditText.setOnKeyListener(this);

    //Intent from main activity
    Intent i = getIntent();
    String urlType = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.TYPE));
    final String url = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.URL));
    String title = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.TITLE));
    String searchedText = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.SEARCHED_TEXT));

    fragCounter = 0;
    fragTags = new ArrayList<>();

    //Fragment opening
    if (savedInstanceState == null){
        openFragment(url, EnumVal.FragStatus.NEW, null);
    }

    //AdMob Ads
    if (!BuildConfig.PAID_VERSION){
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_webview));
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                if(ConsentInformation.getInstance(WebsiteView.this).getConsentStatus() ==
                        ConsentStatus.NON_PERSONALIZED){
                    Bundle extras = new Bundle();
                    extras.putString("npa", "1");
                    mInterstitialAd.loadAd(new AdRequest.Builder()
                            .addNetworkExtrasBundle(AdMobAdapter.class,extras).build());
                } else {
                    mInterstitialAd.loadAd(new AdRequest.Builder().build());
                }
            }
        });
        if(ConsentInformation.getInstance(WebsiteView.this).getConsentStatus() == ConsentStatus.NON_PERSONALIZED){
            Bundle extras = new Bundle();
            extras.putString("npa", "1");
            mInterstitialAd.loadAd(new AdRequest.Builder()
                    .addNetworkExtrasBundle(AdMobAdapter.class,extras).build());
        } else {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    }

    //Toolbar search edit text will be enabled in below case
    if (urlType.equals(EnumVal.Type.SEARCHED_TEXT.toString())){
        searchBarLayout.setVisibility(View.VISIBLE);
        searchIcon.setImageResource(R.drawable.close_icon);
        searchEditText.setText(searchedText);
        searchEditText.clearFocus();
        searchEditText.setCursorVisible(false);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        if (searchEditText.hasFocus())
            searchEditText.setCursorVisible(true);

    }


    searchEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            clearEditText.setVisibility(View.VISIBLE);
        }
    });

    //Navigation view in toolbar
    navRecView.setLayoutManager(new LinearLayoutManager(this));

    mViewModel = ViewModelProviders.of(this).get(DbViewModel.class);
    mViewModel.getSitesWithClicksByIsShown().observe(this, new Observer<List<SitesWithClicks>>() {
        @Override
        public void onChanged(@Nullable List<SitesWithClicks> sitesWithClicks) {
            if (sitesWithClicks!= null && sitesWithClicks.size()>0){
                int reArrSite = prefSingleton.getStorage().getInt(Constants.STORAGE_REARRANGESITE_NAV,
                        Constants.SORTING_PRIORITY);

                sitesWithClicks = Utility.sortSitesData(reArrSite, sitesWithClicks);
                navAdapter = new ListRecViewAdapter(EnumVal.DialogType.NAVBAR_ITEMS,
                        sitesWithClicks, WebsiteView.this);
                navRecView.setAdapter(navAdapter);
            }
        }
    });

}

开放片段方法:

public void openFragment(String url, EnumVal.FragStatus fragStatus, String toOpenTag){
    if(navFrame.getVisibility() == View.VISIBLE)
        downArrow.performClick();

    if(fragStatus == EnumVal.FragStatus.NEW){
        String fragTag = getNextFragTag();

        WebsiteViewFragment fragment = WebsiteViewFragment.newInstance(url, fragTag);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if(fragCounter == 1){
            fragmentTransaction.replace(R.id.fragment, fragment, fragTag);
            currentFrag = fragTag;
        } else {
            if(getSupportFragmentManager().findFragmentByTag(currentFrag) != null){
                fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
                fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag(currentFrag));
            }

            fragmentTransaction.add(R.id.fragment, fragment, fragTag);
            currentFrag = fragTag;
        }
        fragmentTransaction.commit();
        fragTags.add(new TabDetail(fragTag,"Tab - 1",null));

    } else if (fragStatus == EnumVal.FragStatus.OLD){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if(getSupportFragmentManager().findFragmentByTag(currentFrag) != null){
            fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
            fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag(currentFrag));
        }

        fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag(toOpenTag));
        fragmentTransaction.commit();
        currentFrag = toOpenTag;
    }
}

并且Fragment中只有Webview,其中Webview正在加载许多Web设置。但是事情一直持续到片段耗费时间,例如用户从主要活动中点击,大约0.6秒钟后,片段正在打开,而具有片段的活动仅需0.1秒才能打开,所以这个时间(可能)与附加片段或其他东西有关?

任何人都可以向我解释,我在哪里犯错?

我对此有疑问:

  1. 问题是网页视图(片段中),其中包含一堆Web设置吗?
  2. 将片段附加到活动2需要更多时间吗?
  3. activity-2的UI部分是否需要时间(例如具有4个按钮的工具栏设置),并且在该片段上附加了该片段之后,这会导致更多的时间消耗?
  4. 还是正常情况?

有人可以请我说明将整个任务的时间缩短到<0.2秒的方法吗?预先感谢。

0 个答案:

没有答案