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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:38:02+00:00 2026-06-10T11:38:02+00:00

I am developing an android app. If I press a back button the state

  • 0

I am developing an android app. If I press a back button the state of my application should be saved .What should i use to save the state ..am confused with all of these onPause(),onResume(), or onRestoresavedInstance() ??? which of these should i use to save the state of my application?? For eg when i press exit button my entire app should exit i have used finish() ?

   public void onCreate(Bundle savedInstanceState)
   {   

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    s1=(Button)findViewById(R.id.sn1);
    s1.setOnClickListener(this);
    LoadPreferences();
    s1.setEnabled(false);
    }

    public void SavePreferences()
 {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("state", s1.isEnabled());
       }
 public void LoadPreferences()
 {
     System.out.println("LoadPrefe");
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        Boolean  state = sharedPreferences.getBoolean("state", false);
        s1.setEnabled(state);
       }
 @Override
 public void onBackPressed()
 {
    System.out.println("backbutton");
    SavePreferences();
     super.onBackPressed();
 }
  • 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-10T11:38:03+00:00Added an answer on June 10, 2026 at 11:38 am

    What you have to do is, instead of using KeyCode Back, you have override the below method in your Activity,

    @Override
    public void onBackPressed() {
    
        super.onBackPressed();
    }
    

    And save the state of your Button using SharedPrefrence, and next time when you enter your Activity get the value from the Sharedpreference and set the enabled state of your button accordingly.

    Example,

    private void SavePreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("state", button.isEnabled());
        editor.commit();   // I missed to save the data to preference here,. 
       }
    
       private void LoadPreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        Boolean  state = sharedPreferences.getBoolean("state", false);
        button.setEnabled(state);
       }
    
       @Override
    public void onBackPressed() {
        SavePreferences();
        super.onBackPressed();
    }
    
    onCreate(Bundle savedInstanceState)
    {
       //just a rough sketch of where you should load the data
        LoadPreferences();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an android app, and not understanding the back button. There is an
I' m developing an Android app wich requires me to use a Database. After
I am developing an Android app. I have a custom button with a .9.png
I am developing Android TextToSpeech app. This application is not working in Android 1.5
I have almost finished developing an Android App. I use the GPS location and
hi guys i have been developing android app that should match most of the
I simply thought I could use jython for developing Android App. But it was
I'm new to proguard, and developing android app, after building with default android proguard
Iam developing an android app.about google maps API for android i want to ask
I've been developing an Android app using Eclipse and the Android plug-in for Eclipse

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.