简体   繁体   English

Android Studio 刷新活动并显示当前片段

[英]Android Studio refresh the activity and display the current fragment

I am just starting with android studio, so excuse me for any mistakes.我刚从 android 工作室开始,如有错误请见谅。 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.我已经提出了一些方法来做到这一点,但我试图简化它,看看我是否可以通过使用 onOptionsItemSelected 方法来做到这一点。 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.活动重新启动,但它显示了我在 onCreate 方法上分配的默认片段。 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.如果我可以让活动从 onResume 方法开始加载,而不是再次 onCreate,那将是很棒的,我认为它会显示当前片段。 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.编辑:这是 onCreate 中的代码,我在这段代码的末尾添加了片段。 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);
    }
}

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而不是使用相同的活动开始新的,你应该调用recreate并在onCreate方法中你必须在初始化默认片段之前检查savedInstanceState == null

@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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM