Search code examples
androideclipseforeveractivity-finish

close an activity forever on bottun click


My android app has an users' guide activity which should only start in the first run. It has a button. I wanna use the button to close the activity forever. I used this code:

    Button b1001 = (Button) findViewById(R.id.button1001);
    b1001.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Splash.this, MainActivity.class));finish();


        }
    });

It doesen't work. I heard I should use sharedpre/something/ but I Can't. I need a sample code. thnx


Solution

  • Refer to this link: http://developer.android.com/guide/topics/data/data-storage.html

    It's quite clear and doesn't require extensive Android knowledge to implement. Basically you will be storing a value the first time you run the "tutorial", and the next time the user runs the app, the "tutorial" will not display.

    So, this will be used to save the preferences:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("silentMode", mSilentMode);  
    editor.commit();
    

    And this will be used to retrieve the previously stored:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean silent = settings.getBoolean("silentMode", false);
    

    Hope it helps!