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

huangapple go评论64阅读模式
英文:

Android Studio refresh the activity and display the current fragment

问题

I am just starting with android studio, so excuse me for any mistakes. 我刚开始使用Android Studio,所以请原谅我可能会犯的错误。

I am trying to switch the language of the app by using spinner menu. 我正在尝试通过使用下拉菜单来切换应用程序的语言。

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: 这是我尝试过的:

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. 编辑:这是onCreate中的代码,我在此代码的末尾添加了片段。但它是主页片段,所以每当我使用我的方法“刷新”活动时,它都会加载相同的片段。

英文:

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);
    }
}

答案1

得分: 1

在相同的活动中,不要重新开始,而应该调用 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) {
        // 在此处初始化默认片段
    }
}
英文:

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

huangapple
  • 本文由 发表于 2020年7月31日 08:12:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63183132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定