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...