重新启动导航抽屉片段活动的适当方法?

时间:2014-12-30 22:15:24

标签: android android-activity android-fragments android-fragmentactivity

嗯,我正在开发一个应用程序,经过多次尝试后,我无法恢复后被另一个具有更高优先级或更高资源需求的应用程序销毁,所以现在我选择关闭它并重新启动整个活动但是我面临另一个问题。每当我重新开始这个洞的事情,后来通过退出活动我发现旧的活动仍然打开所以重启操作发生两次,即使最后退出第二次我终于回到android家我仍然没有得到如何继续发生。 现在我正在关闭并在片段的onCreateView()方法结束时重新启动应用程序。在这一点上可以做到这一点还是有更好的方法来做我想做的事情?

    public void onSectionAttached(int number) {
    try {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                theFragment = 1;
                if (isOnline()) {
                    if (bluClient.gotoHomePage(mainWebBrowser)) {
                        needloading = 6;
                        loadingdialog = ProgressDialog.show(this, "", "Attendere...", true, false);
                    }
                } else {
                    Toast.makeText(this, "Rete non disponibile", Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                theFragment = 2;
                if (isOnline()) {
                    if (bluClient.gotoUrlPage(mainWebBrowser)) {
                        needloading = 7;
                        if (bluClient.getCurr_page() == 3)
                            nlu = true;
                        loadingdialog = ProgressDialog.show(this, "", "Attendere...", true, false);
                    } else
                        needloading = -2;
                } else {
                    Toast.makeText(this, "Rete non disponibile", Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                theFragment = 3;
                if (isOnline()) {
                    if (bluClient.gotoPrefPage(mainWebBrowser)) {
                        needloading = 8;
                        loadingdialog = ProgressDialog.show(this, "", "Attendere...", true, false);
                    } else
                        needloading = -3;
                } else {
                    Toast.makeText(this, "Rete non disponibile", Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            case 4:
                mTitle = getString(R.string.title_section4);
                theFragment = 4;
                break;
        }
    }catch (NullPointerException e){}
}

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(returnFragment(), container, false);
            TextView t = (TextView) rootView.findViewById(R.id.txtContatore);
        RelativeLayout rel;
        try {
        switch (theFragment) {
                case 1:
                    t.setText(formatBlurumPunti(bluClient.getPoints()));
                    final AutoCompleteTextView aCTV = (AutoCompleteTextView) rootView.findViewById(R.id.txtMotore);
                    aCTV.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //here is your code

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            // TODO Auto-generated method stub
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            if (s.toString() != "") {
                                //forSuggestions.stopLoading();
                                //forSuggestions.loadUrl("http://suggestqueries.google.com/complete/search?output=toolbar&hl=it&q=" + s.toString());
                                new setGoogleSuggestions(rootView).execute("http://suggestqueries.google.com/complete/search?output=toolbar&hl=it&q=" + s.toString());
                            }
                        }
                    });
                    ImageButton b;
                    for(short k=0;k<6;k++) {
                        switch (k) {
                            case 0:
                                t = (TextView) rootView.findViewById(R.id.txtFirstBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mGoogle);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.gLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                            case 1:
                                t = (TextView) rootView.findViewById(R.id.txtSecondBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mYahoo);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.yLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                            case 2:
                                t = (TextView) rootView.findViewById(R.id.txtThirdBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mBing);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.bLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                            case 3:
                                t = (TextView) rootView.findViewById(R.id.txtFourthBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mYoutube);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.YoLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                            case 4:
                                t = (TextView) rootView.findViewById(R.id.txtFifthBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mVirgilio);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.vLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                            case 5:
                                t = (TextView) rootView.findViewById(R.id.txtSixthBonusengine);
                                b=(ImageButton)rootView.findViewById(R.id.mPaginegialle);
                                if(bluClient.getMotori().get(k).isSelected()) {
                                    rel=(RelativeLayout)rootView.findViewById(R.id.pLayout);
                                    rel.setBackgroundResource(R.drawable.button_selected);
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(bluClient.getMotori().get(k).getImage());
                                    }
                                }
                                else{
                                    if (bluClient.getMotori().get(k).getImage() != null) {
                                        b.setImageBitmap(toGrayscale(bluClient.getMotori().get(k).getImage()));
                                    }
                                }
                                break;
                        }
                        if(bluClient.getMotori().get(k).getValue()>1)
                        {
                            t.setText(Integer.toString(bluClient.getMotori().get(k).getValue()));
                            rel = (RelativeLayout) t.getParent();
                            rel.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case 2:
                    t.setText(formatBlurumPunti(bluClient.getPoints()));
                    final AutoCompleteTextView aCTV2 = (AutoCompleteTextView) rootView.findViewById(R.id.txtMotore);
                    aCTV2.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //here is your code

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            // TODO Auto-generated method stub
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            if (s.toString() != "") {
                                //forSuggestions.stopLoading();
                                //forSuggestions.loadUrl("http://suggestqueries.google.com/complete/search?output=toolbar&hl=it&q=" + s.toString());
                                new setGoogleSuggestions(rootView).execute("http://suggestqueries.google.com/complete/search?output=toolbar&hl=it&q=" + s.toString());                                }
                        }
                    });
                    if(urlHistory=="")
                        urlHistory=theUserSessionManager.restoreUrlHistory();
                    ArrayList<urlwidgStructure>lis;
                    lis=getUrlWidgStructureFromRestoredList(theUserSessionManager.restoreUrlHistoryToArrayList());
                    for(short f=0;f<lis.size();f++)
                    {
                        setUrlVisitato2(rootView,inflater,lis.get(f));
                    }
                    break;
                case 3:
                    t.setText(formatBlurumPunti(bluClient.getPoints()));
                    if (needloading == -3) {
                        ArrayList<Blurum.preferitoBlurum> preferiti = bluClient.getPreferiti();
                        if(preferiti.size()>0) {
                            for (short i = 0; i < preferiti.size(); i++) {
                                setPreferitiOfPage2(rootView, inflater, preferiti.get(i).nome, preferiti.get(i).stelle);
                            }
                            RelativeLayout re = (RelativeLayout) rootView.findViewById(R.id.relPrefPageContainer);
                            if (!preferiti.isEmpty()) {
                                re.setVisibility(View.VISIBLE);
                                ImageButton btnFoward = (ImageButton) re.findViewById(R.id.imgFoward), btnBack = (ImageButton) re.findViewById(R.id.imgBack);
                                if (preferiti.get(0).canGo('f'))
                                    btnFoward.setVisibility(View.VISIBLE);
                                else
                                    btnFoward.setVisibility(View.INVISIBLE);
                                if (preferiti.get(0).canGo('b'))
                                    btnBack.setVisibility(View.VISIBLE);
                                else
                                    btnBack.setVisibility(View.INVISIBLE);
                                t = (TextView) rootView.findViewById(R.id.txtPrefPageNum);
                                String d = Short.toString(preferiti.get(0).getParentPrefPageCurrentPageNum());
                                t.setText(d);
                            } else
                                re.setVisibility(View.INVISIBLE);
                        }
                        else {
                            t=(TextView)rootView.findViewById(R.id.txtNopref);
                            t.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case 4:
                    WebView wvcatalogo=(WebView)rootView.findViewById(R.id.wvCatalogo);
                    wvcatalogo.getSettings().setJavaScriptEnabled(true);
                    wvcatalogo.getSettings().setDomStorageEnabled(true);
                    wvcatalogo.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
                    wvcatalogo.getSettings().setSupportMultipleWindows(true);
                    wvcatalogo.getSettings().setUserAgentString(mainWebBrowser.getSettings().getUserAgentString());
                    wvcatalogo.loadUrl("url");
                    wvcatalogo.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            super.shouldOverrideUrlLoading(view,url);
                            return false;
                        }
                    });
                    break;
            }

        }
        catch (NullPointerException e)
        {
            //Once my static class variable seem to be Null I know that the activity is trying
            //to restore its self so I have to do this

            Intent intent = new Intent(getActivity(), Accesso.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        }
        return rootView;
    }    

对于凌乱的代码感到抱歉......

1 个答案:

答案 0 :(得分:0)

我最终在catch子句中显示了一个DialogFragment,告诉用户应用程序需要重新启动,按下正面按钮我执行上一个catch子句中的相同代码...我不知道为什么以前的解决方案给了我这种奇怪的行为,因为现在我已经添加了一个对话框,并且应用程序正在重新启动...