如何在重新启动应用程序后恢复以前的活动?

时间:2016-01-03 10:20:20

标签: java android android-studio

我的应用上有这种语言设置。在我设置了所需的语言并重新启动后,语言不适用,而是重新设置为默认语言。任何人都可以帮我这个吗?

这是我的代码



import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;


public class AndroidLocalize extends Activity {

  Spinner spinnerctrl;
  Button btn;
  Locale myLocale;

  @
  Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidlocalize);
    spinnerctrl = (Spinner) findViewById(R.id.spinner1);
    spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

      public void onItemSelected(AdapterView <? > parent, View view,
        int pos, long id) {

        if (pos == 1) {

          Toast.makeText(parent.getContext(),
              "You have selected Filipino", Toast.LENGTH_SHORT)
            .show();
          Intent nextForm = new Intent(".MainActivity");
          startActivity(nextForm);
          setLocale("tl");


        } else if (pos == 2) {

          Toast.makeText(parent.getContext(),
              "You have selected English", Toast.LENGTH_SHORT)
            .show();
          Intent nextForm = new Intent(".MainActivity");
          startActivity(nextForm);
          setLocale("en");

        }
      }

      public void onNothingSelected(AdapterView <? > arg0) {
        // TODO Auto-generated method stub

      }
    });
  }

  public void setLocale(String lang) {

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, AndroidLocalize.class);
    startActivity(refresh);

  }
}
&#13;
&#13;
&#13;

非常感谢!

1 个答案:

答案 0 :(得分:0)

创建AndroidLocalize时,应检查配置是否存在,然后使用该配置启动MainActivity。

你正在做的是设置你再次启动相同的激活,因为在创建方法你没有检查设置配置它将显示默认值

溶液, 1.检查config oncreate 2.首先设置语言环境,然后调用startActivity