Opening the last fragment view in Java Android Apps

26 views Asked by At

I created a bottom navigation for my app and was wondering how could I make it so when the user opens the app, it returns to the last fragment they were viewing? At the moment even if you just switch apps for a second, it reverts to the home page immediately. Thanks in advance!

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomnav = findViewById(R.id.bottom_menu);
        bottomnav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit();


       mediaPlayer = MediaPlayer.create(this, R.raw.clickandboop);


    }

    private final BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;


                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();
                            mediaPlayer.start();
                            break;
                        case R.id.nav_favourites:
                            selectedFragment = new FavFragment();
                            mediaPlayer.start();
                            break;
                        case R.id.nav_trophies:
                            selectedFragment = new TrophiesFragment();
                            mediaPlayer.start();
                            break;
                        case R.id.nav_info:
                            selectedFragment = new InfoFragment();
                            mediaPlayer.start();
                            break;


                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, selectedFragment).commit();

                    return true;
                }
            };
2

There are 2 answers

0
ianhanniballake On BEST ANSWER

Fragments already restore you back to exactly where you were.

This means that you need to wrap your replace() call with a check of savedInstanceState == null to avoid destroying the just recreated Fragment:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment_layout, new HomeFragment())
        .commit();
}
0
DemoDemo On

Just use sharedprefs and store the last fragment that the user was in. Use the onStop callback of the fragment to store it. When the user opens the application just check this value if it's not set open homefragment otherwise just open the last fragment based on the value you have in your shared preferences