Search code examples
javaandroidandroid-studioandroid-fragmentsonresume

Android Studio refresh the activity and display the current fragment


I am just starting with android studio, so excuse me for any mistakes. I am trying to switch the language of the app by using spinner menu. I have come over some methods to do this, but I was trying to simplify it and see if I can make this just by using onOptionsItemSelected method. Everything works fine and the language changes in the app, but only if I load another fragment in this activity. I was looking for a solution how to refresh the activity so that the user doesn't have to click any other button to see that the language has changed. I came up with a solution to finish the activity and make the intent to start it again, but here the problem starts. The activity restarts but it shows the default fragment which I assigned on the onCreate method. I don't know how to make it load the fragment which the user was currently seeing. It would be great if I could make the activity start loading from the onResume method, instead of onCreate again, I think it would show the current fragment. Is it even possible to do this? Is there any other easy way to make it show the current fragment with the new language? Maybe there's no easy way and I should start all over again? Please help me. This is what I've tried:

     @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    Configuration configuration = new Configuration();
    Intent intent = new Intent();
    switch (item.getItemId()){
        case R.id.action_polski:
            Locale localePl = new Locale("pl");
            Locale.setDefault(localePl);
            configuration.locale = localePl;
            getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());

            finish();
            startActivity(new Intent(this, MainActivity.class));
            overridePendingTransition(0, 0);

            break;
        case R.id.action_english:
            Locale localeEn = new Locale("en");
            Locale.setDefault(localeEn);
            configuration.locale = localeEn;
            getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());

            finish();
            startActivity(new Intent(this, MainActivity.class));
            overridePendingTransition(0, 0);

            break;
    }
    return true;
}        

Unfortunately I can't just refresh the fragment. The entire activity has to be refreshed because otherwise the navigation drawer doesn't change the language, it stays in the previous language.

Edit: This is the code in onCreate, I add the fragment at the end of this code. But it's the home fragment, so whenever I "refresh" the activity with my method it loads the same fragment.

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    hideSystemUI();

    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    navigationView.bringToFront();
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new HomeFragment()).commit();
        navigationView.setCheckedItem(R.id.nav_home);
    }
}

Solution

  • Instead of start new with same Activity you should call recreate and inside onCreate method you have to check savedInstanceState == null before init default fragment

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Configuration configuration = new Configuration();
        Intent intent = new Intent();
        switch (item.getItemId()){
            case R.id.action_polski:
                Locale localePl = new Locale("pl");
                Locale.setDefault(localePl);
                configuration.locale = localePl;
                getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
    
                recreate()
                break;
            case R.id.action_english:
                Locale localeEn = new Locale("en");
                Locale.setDefault(localeEn);
                configuration.locale = localeEn;
                getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
    
                recreate()
                break;
        }
        return true;
    }   
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if (savedInstanceState == null) {
                // init default fragment
            }
        }