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 9069935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:33:28+00:00 2026-06-16T17:33:28+00:00

I am creating a simple Click Counter Android App using Java. I am new

  • 0

I am creating a simple Click Counter Android App using Java. I am new to Java. I’m Trying to save the number of the count upon exit of the application, whether if the back button is pressed or the app is closed or crashes, etc. Here is my code so far:

public class wazeefa extends Activity  {

//Count Button  
TextView txtCount;
Button btnCount;
int count; 
Button wmute;
Button wreset;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wazeefa);

    //SAVE COUNT
    SharedPreferences app_preferences = 
       PreferenceManager.getDefaultSharedPreferences(this);

    count = app_preferences.getInt("count", 0);

    txtCount = (TextView)findViewById(R.id.wcount);
    txtCount.setText("This app has been started " + count + " times.");

    SharedPreferences.Editor editor = app_preferences.edit();
    editor.putInt("count", ++count);
    editor.commit();

    //Button SOUND AND COUNT
    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.wclick);   

    btnCount.setOnClickListener(new OnClickListener() {
        public void onClick(View V) {
        final ImageView image = (ImageView) findViewById(R.id.imageview);
           count++; 
           if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
           if (count > 0) image.setImageResource(R.drawable.duroodimage);
           if (count > 9) image.setImageResource(R.drawable.zikrimage); 
           if (count > 39) image.setImageResource(R.drawable.duroodimage);
           txtCount.setText(String.valueOf(count));
           mpButtonClick.start(); 

    //RESET Button
    wreset = (Button)findViewById(R.id.wreset);       
    wreset.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            count = 0; 
            image.setImageResource(R.drawable.duroodimage);;
               txtCount.setText("0");


    }

I’m having 2 issues with the app.

Firstly the count is not being saved to what it is when the application is closed and then opened again. For example if the count is at ’20’ and I hit the back button, then go back to the same page the count will display as ‘3’. – The funny thing is, that every time I have tried the above and gone back into the application after hitting the back button the count has gone up by 1 count.

The second issue is that when I go back into the app and the count shows as ‘5’ just for example, the Reset button no longer functions – it does nothing. But when I continue the count and then hit the Reset button it changes the count to zero again.

Please can anyone assist in fixing the above 2 issues?

New Code after suggestions:

public class wazeefa extends Activity  {

//Count Button  
TextView txtCount;
Button btnCount;
Button wmute;
Button wreset;
public static int count=0;
SharedPreferences app_preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wazeefa);

    //SAVE COUNT
    SharedPreferences app_preferences = 
            PreferenceManager.getDefaultSharedPreferences(this);

    count = app_preferences.getInt("count", 0);

    txtCount = (TextView)findViewById(R.id.wcount);
    txtCount.setText("This app has been started " + count + " times.");}

protected void onPause() {
     super.onPause();

    // save count value here
   SharedPreferences.Editor editor = app_preferences.edit();
   editor.putInt("count", count);
   editor.commit();

    //SOUND and COUNT
    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.wclick);   

    btnCount.setOnClickListener(new OnClickListener() {
        public void onClick(View V) {
        final ImageView image = (ImageView) findViewById(R.id.imageview);
           count++; 
           if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
           if (count > 0) image.setImageResource(R.drawable.duroodimage);
           if (count > 9) image.setImageResource(R.drawable.zikrimage); 
           if (count > 39) image.setImageResource(R.drawable.duroodimage);
           txtCount.setText(String.valueOf(count));
           mpButtonClick.start(); 

    //RESET Button
    wreset = (Button)findViewById(R.id.wreset);       
    wreset.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            count = 0; 
            image.setImageResource(R.drawable.duroodimage);;
               txtCount.setText("0");


    }

Final part of my code:

 protected void onPause() {
         super.onPause();

    // save count value here

      SharedPreferences.Editor editor = app_preferences.edit();
      editor.putInt("count", count);
      editor.commit();


         };
     });
      }});
  };}
  • 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-16T17:33:29+00:00Added an answer on June 16, 2026 at 5:33 pm

    Declare count as static at class level :

    public static int count=0;
    SharedPreferences app_preferences ;
    

    and use onPause for saving count value in SharedPreferences as :

     protected void onPause() {
         super.onPause();
    
        // save count value here
       SharedPreferences.Editor editor = app_preferences.edit();
       editor.putInt("count", count);
       editor.commit();
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a simple Click Counter Android App using Java. I am new
I'm creating a simple click counter android app, sound is played when a button
I'm creating an app on android in java, very simple, which has a button
I'm trying to write a simple timer app. I'm creating a counter class that
Creating a simple RPG game, first time using XNA. Trying to get my character
I am new to Android programming. I am creating a very simple application in
I am creating a simple Android App, in which the first screen would present
I'm creating simple editor in Java FX using codemirror.js library. I embeded codemirror editor
friends, I am new to Iphone development, I am creating a simple analog clock,
I am creating simple app on Silverlight 4. In folder of View I have

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.