簡體   English   中英

Android Studio 刷新活動並顯示當前片段

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

我剛從 android 工作室開始,如有錯誤請見諒。 我正在嘗試使用微調器菜單切換應用程序的語言。 我已經提出了一些方法來做到這一點,但我試圖簡化它,看看我是否可以通過使用 onOptionsItemSelected 方法來做到這一點。 一切正常,應用程序中的語言發生變化,但前提是我在此活動中加載另一個片段。 我一直在尋找如何刷新活動的解決方案,以便用戶無需單擊任何其他按鈕即可查看語言已更改。 我想出了一個解決方案來完成活動並打算重新開始它,但問題就從這里開始了。 活動重新啟動,但它顯示了我在 onCreate 方法上分配的默認片段。 我不知道如何讓它加載用戶當前看到的片段。 如果我可以讓活動從 onResume 方法開始加載,而不是再次 onCreate,那將是很棒的,我認為它會顯示當前片段。 甚至有可能做到這一點嗎? 有沒有其他簡單的方法可以讓它用新語言顯示當前片段? 也許沒有簡單的方法,我應該重新開始? 請幫我。 這是我嘗試過的:

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

不幸的是,我不能只刷新片段。 必須刷新整個活動,否則導航抽屜不會更改語言,它會保持以前的語言。

編輯:這是 onCreate 中的代碼,我在這段代碼的末尾添加了片段。 但它是主片段,所以每當我用我的方法“刷新”活動時,它都會加載相同的片段。

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

而不是使用相同的活動開始新的,你應該調用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