0

Well I am working on an app that after several tries I am not able to restore after it get destroyed by another app with higher priority or higher need of resources so now I am choosing to close it and restart the whole activity but I am facing another issue. Whenever I restart the hole thing, later by exiting the activity I found out that the old activity it's still opened so the restart operation happens twice and even if at the end by exiting for the second time I finally return to the android home I am still not getting how this keeps on happening. Right now I am closing and restarting the app at the end of the onCreateView() method of the fragment. Is it ok doing it in that point or there are better ways to do what I am trying to do ?

    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;
    }    

Sorry for the messy code...

n1nsa1d00
  • 856
  • 9
  • 23
  • I didn't get what you're trying to achieve. Could you post some code? – natario Dec 30 '14 at 23:03
  • Here it is, this video capture I made would help you understand it better [link](https://www.youtube.com/watch?v=v-Mkz00cl0I).. I will also edit mi question with some code.. – n1nsa1d00 Dec 31 '14 at 08:38
  • Sorry but i dont understand what you are trying to do either. What variable is null and where does it come from? It sounds like whatever you are trying to do should be handled in a completely different more appropriate way than restarting an activity – tyczj Dec 31 '14 at 15:24
  • The null variable is a static webView that I am not able to restore not even by reloading the url... – n1nsa1d00 Dec 31 '14 at 15:30
  • @MarcoDufal well can you show that code since that is obviously the problem – tyczj Dec 31 '14 at 15:43
  • you should read and *understand* this: http://developer.android.com/training/basics/activity-lifecycle/recreating.html – passsy Dec 31 '14 at 15:45
  • I've tried that saving the state of the webview but restoring it I was trowed a nullpointerexception... – n1nsa1d00 Dec 31 '14 at 15:55
  • @tyczj I've added more code.. – n1nsa1d00 Dec 31 '14 at 16:06

1 Answers1

0

I ended up showing a DialogFragment in the catch clause telling the user that the app needs to restart and by pressing the positive button I execute the same code that was in the previous catch clause... I sill don't get why the previous solution was giving me that strange behavior cause now I've get added a dialog and the app it's restarting as wanted..

n1nsa1d00
  • 856
  • 9
  • 23