1

In my application, I have a settings Activity to pick a language that should run on the first time only of the app, however it's showing sometimes when i switch fragments. How do I make it show only the first time of the run?

code: MainActivity

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

    }else{
   ... start the normal mainactivity
  }

the firstTime fucntion:

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

the settings activity:

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

        }
    });
}
George2456
  • 340
  • 5
  • 19
  • you should finish the activity when you are done. I.e. after starting the new one – Tim Jan 09 '17 at 14:46
  • @TimCastelijns So should just add finish(); in the Settings activity after launching the startActivity(mainIntent);? – George2456 Jan 09 '17 at 14:53

1 Answers1

0

You should write finish(); after starting the other activity. And don't forget to change the preferences.getBoolean("RanBefore1", false); and make it equal to true. I am assuming that you want it to be true after selecting the language. It starts because you didn't call finish();.

Omar
  • 105
  • 1
  • 3
  • 12
  • I ended up adding it after going back to the mainacitivity from the settingd activity, and Im changing the RanBefore1 to false. – George2456 Jan 09 '17 at 23:20