I am a beginner in android and I am not able to understand why I am getting error. When I am clicking on the home button(Bottom Navigation bar) then I am getting this error. I am using shared preference for saving my value when but when this value is retrieved then an error is coming.
MyActivityCode:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLastLocation();
binding.bottmNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home1: //when i m clicking here
setFragment(homeFragment);
SharedPreferences shprefs = getSharedPreferences(prefs, MODE_PRIVATE);
SharedPreferences mprefs = getSharedPreferences(myprefs, MODE_PRIVATE);
String savedvalue = shprefs.getString("savedvalue", null);
String savedcountry = mprefs.getString("savedcountry", null);
if ((savedvalue != null && savedvalue.equals("India")) || savedcountry.equals("India")) {
newsModels.clear();
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
setindianewsdata();
setindiashopdata();
setFragment(homeFragment);
} else if (savedvalue.equals("Usa") || savedcountry.equals("Usa")) {
newsModels.clear();
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
}
return true;
case R.id.location:
final AlertDialog.Builder mbuilder = new AlertDialog.Builder(HomeActivity.this);
View v = getLayoutInflater().inflate(R.layout.dialog_layout, null);
Button okbutton = v.findViewById(R.id.okbutton);
final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter < String > adapter = new ArrayAdapter < String > (HomeActivity.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.countries_list));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mbuilder.setView(v);
final AlertDialog dialog = mbuilder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences(prefs, MODE_PRIVATE).edit();
editor.putString("savedvalue", spinner.getSelectedItem().toString());
editor.commit();
String text = spinner.getSelectedItem().toString();
newsModels = new ArrayList < > ();
if (text.equals("India")) {
newsModels.clear();
setindianewsdata();
setindiashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();
} else if (text.equals("Usa")) {
newsModels.clear();
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();
}
}
});
}
return true;
}
});
}
public void getLastLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {
progressBar.setVisibility(View.VISIBLE);
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener < Location > () {
@Override
public void onComplete(@NonNull Task < Location > task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
countryname = findcountry(location.getLatitude(), location.getLongitude());
handlevalue(countryname);
if (countryname.equals("India")) {
newsModels.clear();
setindianewsdata();
setindiashopdata();
TypedArray socialimg = getResources().obtainTypedArray(R.array.social_image);
String[] socialname = getResources().getStringArray(R.array.social_name);
String[] socialhref = getResources().getStringArray(R.array.social_url);
socialModels = new ArrayList < > ();
for (int k = 0; k < socialimg.length(); k++) {
SocialModel socialModel = new SocialModel(socialimg.getResourceId(k, 0), socialname[k], socialhref[k]);
socialModels.add(socialModel);
}
SocialSingleton.getConstant().addNewsModel(socialModels);
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
progressBar.setVisibility(View.GONE);
} else if (countryname.equals("USA")) {
TypedArray indianewslogo = getResources().obtainTypedArray(R.array.usnews);
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
progressBar.setVisibility(View.GONE);
}
}
}
}
);
} else {
Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
} else {
requestPermissions();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}
}
When I am clicking on the home button then I am getting this error. Don't know why.
java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 2b2916b8-40c2-4190-b2d7-e6440a7592eb
at androidx.fragment.app.FragmentManagerImpl.getFragment(FragmentManagerImpl.java:365)
at androidx.fragment.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:309)
at androidx.viewpager.widget.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
at android.view.View.dispatchRestoreInstanceState(View.java:18042)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3859)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
at android.view.View.restoreHierarchyState(View.java:18020)
at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:548)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:907)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at
seting setSaveEnabled() to false into our viewpager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With