Reputation: 85
I face a problem, I have an activity named "MainActivity" which have 4 fragments. The problem is if I'm in fragment 2 OR 3 OR 4, and the app goes to the background like:
If I open bottom navigation and app go to the background, after the bottom navigation close it redirect me to the 1st fragment(Default fragment).
If I minimize the app and then resume it again go to 1st fragment
If my mobile goes to sleep and then I on it again redirect me to 1st fragment.
I cannot understand why this behaviour comes out. Please suggest me the possible solutions.
Here is the main activity code :
public class ContainerActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener{
private Fragment fragment;
private BottomNavigationView bottomNavigationView;
private RestaurantListFragment restaurantListFragment;
private String completeAddress, cityName, countryName, latitude, longitude, nickName, addressLabel, locationType, currentLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
if(getIntent().getExtras() != null){
latitude = getIntent().getExtras().getString("latitude");
longitude = getIntent().getExtras().getString("longitude");
completeAddress = getIntent().getExtras().getString("complete_address");
cityName = getIntent().getExtras().getString("city_name");
countryName = getIntent().getExtras().getString("country_name");
nickName = getIntent().getExtras().getString("nick_name");
addressLabel = getIntent().getExtras().getString("label");
locationType = getIntent().getExtras().getString("location_type");
currentLock = getIntent().getExtras().getString("current_lock");
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
bundle.putString("longitude", longitude);
bundle.putString("complete_address", completeAddress);
bundle.putString("city_name", cityName);
bundle.putString("country_name", countryName);
bundle.putString("nick_name", nickName);
bundle.putString("label", addressLabel);
bundle.putString("location_type", locationType);
bundle.putString("current_lock", currentLock);
restaurantListFragment.setArguments(bundle);
}
}
@Override
protected void onResume() {
super.onResume();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, restaurantListFragment).addToBackStack(null).commit();
}
private void initView() {
bottomNavigationView = findViewById(R.id.btoom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
bottomNavigationView.setSelectedItemId(R.id.home);
restaurantListFragment = new RestaurantListFragment();
}
public void openRestaurantMenuFragment(Bundle bundle) {
RestaurantMenuFragment restaurantMenuFragment = new RestaurantMenuFragment();
restaurantMenuFragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, restaurantMenuFragment)
.addToBackStack(null)
.commit();
}
public void openFoodListOrDetailFrgment() {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new FoodDetailFragment())
.addToBackStack(null)
.commit();
}
public void openCart(){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new MyOrderFragment())
.addToBackStack(null)
.commit();
}
public void openCompleteItemDetailFragment(){
bottomNavigationView.setVisibility(View.GONE);
}
public void showBottomNavigation(){
if(bottomNavigationView.getVisibility() == View.GONE){
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
switch (item.getItemId()) {
case R.id.home:
if ((getSupportFragmentManager().findFragmentById(R.id.fragment_container)) != null && !(fragment instanceof RestaurantListFragment)){
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new RestaurantListFragment()).addToBackStack(null).commit();
}
break;
case R.id.explore:
break;
case R.id.profile:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new ProfileFragment())
.addToBackStack(null)
.commit();
break;
}
return true;
}
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if(fragment instanceof RestaurantListFragment){
getSupportFragmentManager().popBackStack(0, getSupportFragmentManager().POP_BACK_STACK_INCLUSIVE);
}
if(fragment instanceof CompleteFoodItemDetailFragment || fragment instanceof GoogleMapFragment){
if(bottomNavigationView.getVisibility() == View.GONE){
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
super.onBackPressed();
}
}
Upvotes: 0
Views: 1170
Reputation: 529
In your Question, you are saying when activity is resumed from the background the default fragment opens (restaurantListFragment) I am supposing. it's due to
onResume() Method
@Override
protected void onResume() {
super.onResume();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, restaurantListFragment).addToBackStack(null).commit();
}
replace it with(fragment will only be changed if it's empty or detached at some point)
private fm;
initViews(){
fm = supportFragmentManager.beginTransaction();
}
@Override
protected void onResume() {
super.onResume();
if(fm.isEmpty() || restaurantListFragment.isDetached()){
fm.replace(R.id.fragment_container, restaurantListFragment).addToBackStack(null).commit();
}
}
also replace the fragment in onCreate
@Override
protected void onCreate(Bundle savedInstanceState){
...
//below initViews();
...
fm.replace(R.id.fragment_container, restaurantListFragment).addToBackStack(null).commit();
}
Upvotes: 2