Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序运行时显示首次运行活动_Android - Fatal编程技术网

Android 应用程序运行时显示首次运行活动

Android 应用程序运行时显示首次运行活动,android,Android,在我的应用程序中,我有一个设置活动来选择一种只应在应用程序第一次运行时才运行的语言,但有时在切换片段时它会显示出来。如何让它只在第一次跑步时显示 代码:MainActivity if (isFirstTime()) { Intent myIntent; myIntent = new Intent(this, Settings.class); startActivity(myIntent); }else{ ... start

在我的应用程序中,我有一个设置活动来选择一种只应在应用程序第一次运行时才运行的语言,但有时在切换片段时它会显示出来。如何让它只在第一次跑步时显示

代码:MainActivity

     if (isFirstTime()) {
        Intent myIntent;
        myIntent = new Intent(this, Settings.class);
        startActivity(myIntent);

    }else{
   ... start the normal mainactivity
  }
第一次功能:

  private boolean isFirstTime() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean ranBefore = preferences.getBoolean("RanBefore1", false);
    return !ranBefore;
}
设置活动:

    private Spinner spinner;
private Button button;
private String lang="";

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


    setContentView(R.layout.settings_layout);
    findViewByID();
    setListeners();

}

private void findViewByID() {
    spinner = (Spinner) findViewById(R.id.spinner);
    button = (Button) findViewById(R.id.button2);
    ArrayAdapter adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.langs));



    spinner.setAdapter(adapter);

}

private void setListeners() {
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            switch (position) {
                case 0:
                    lang= "en";
                    break;
                case 1:
                    lang="fr";
                    break;
                case 2:
                    lang="ar";
                    break;
            }            }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("RanBefore1", true);
            editor.apply();
            if(getLanguage()!=null&&!getLanguage().contains(lang)) {
                setLanguage(lang);

                Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(mainIntent);
            }else {

                Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(mainIntent);
            }

        }
    });
}
专用微调器微调器;
私人按钮;
私有字符串lang=“”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings\u布局);
findViewByID();
setListeners();
}
私有void findViewByID(){
微调器=(微调器)findViewById(R.id.spinner);
按钮=(按钮)findViewById(R.id.button2);
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_微调器_项,getResources().getStringArray(R.array.langs));
旋转器。设置适配器(适配器);
}
私有void setListeners(){
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
开关(位置){
案例0:
lang=“en”;
打破
案例1:
lang=“fr”;
打破
案例2:
lang=“ar”;
打破
}            }
@凌驾
未选择公共无效(AdapterView父视图){
//你的代码在这里
}
});
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
SharedReferences preferences=PreferenceManager.GetDefaultSharedReferences(getBaseContext());
SharedReferences.Editor=首选项.edit();
putBoolean(“RanBefore1”,true);
editor.apply();
如果(getLanguage()!=null&&!getLanguage()包含(lang)){
语言;
Intent maintent=新的Intent(getApplicationContext(),MainActivity.class);
星触觉(主旨);
}否则{
Intent maintent=新的Intent(getApplicationContext(),MainActivity.class);
星触觉(主旨);
}
}
});
}

您应该编写
finish()启动其他活动后。不要忘记更改
preferences.getBoolean(“RanBefore1”,false)
并使其等于
true
。我假设在选择语言后,您希望它是真实的。
它启动是因为您没有调用
finish()

您应该编写
finish()启动其他活动后。不要忘记更改
preferences.getBoolean(“RanBefore1”,false)
并使其等于
true
。我假设在选择语言后,您希望它是真实的。
它启动是因为您没有调用
finish()

完成后,您应该完成活动。即在启动新的one@TimCastelijns因此,只需添加finish();在启动startActivity(mainIntent)后的设置活动中;?完成后,你应该完成活动。即在启动新的one@TimCastelijns因此,只需添加finish();在启动startActivity(mainIntent)后的设置活动中;?我在从settingd活动返回MainActivity后添加了它,并将RanBefore1更改为false。我在从settingd活动返回MainActivity后添加了它,并将RanBefore1更改为false。