Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:56:11+00:00 2026-06-13T05:56:11+00:00

This is my code: package com.example.sharedprefs; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View;

  • 0

This is my code:

package com.example.sharedprefs;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void doThis (View view){
        i++;
        SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putInt("userChoice",i);
        prefEditor.commit();
        int number = sharedPref.getInt("userChoice", 0);
        Toast.makeText(getApplicationContext(), number + "" , Toast.LENGTH_LONG).show();
        this.finish();

    }

}

When the app runs and I run it again. I constantly get “1” as a toast. I’m trying to get 1 and 2 and 3 and so on. Any ideas? I know that it’s not the program, it’s the “programmer” at this point. Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:56:12+00:00Added an answer on June 13, 2026 at 5:56 am

    Answer given in the first comment; SharedPreferences Logical Error.

    You should put the following code

    SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
    i = sharedPref.getInt("userChoice", 0); 
    

    in the onCreate()-method in your Activity.

    In your code, i is 0 every time you start this Activity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
So, I have this code: package com.example.ponto_2d; import android.app.Activity; import android.view.Menu; import android.view.MotionEvent; import
This is my MyTasteActivity code: package MyTaste; import android.app.Activity; import android.content.Context; import android.os.Bundle; import
This is my code: package com.example.testcode; import android.app.ListFragment; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader;
My code: package com.example.linkingpage; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent;
package com.example.helloandroid; import java.util.ArrayList; import java.util.Arrays; import android.app.TabActivity; import android.os.Bundle; import android.view.Window; import android.widget.ArrayAdapter;
Here is my main: package com.example.frags; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.Fragment;
I have this code: package com.example.pr; import android.media.MediaPlayer; public class Audio{ MediaPlayer mp; public
This is my code: package hello.project; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.opengl.GLSurfaceView.Renderer;
here is my Code package com.example.messenger; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.