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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:16:44+00:00 2026-06-12T10:16:44+00:00

I am trying to detect if my app has been run before, by using

  • 0

I am trying to detect if my app has been run before, by using this code:

(This is in my default Android activity)

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        Log.w("activity", "first time");
        setContentView(R.layout.activity_clean_weather);
    } else {

        Log.w("activity", "second time");
        setContentView(R.layout.activity_clean_weather);
    }


 }

When I first run the app it says first time, when I run it a second time, first time, and a third, first time….

I am using an actual Android device and I am not using the run button each time. I run the app once with the Eclipse run button, then I close the app and press on its icon on my phone.

Is there something wrong with my code?

  • 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-12T10:16:45+00:00Added an answer on June 12, 2026 at 10:16 am

    savedInstanceState is more for switching between states, like pausing/resuming, that kind of thing. It must always be created by you, also.

    What you want in this case is SharedPreferences.

    Something like this:

    public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't change this after it's saved something
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
        boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"
    
        if (firstRun) {
            Log.w("activity", "first time");
            setContentView(R.layout.activity_clean_weather);
    
            SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
            editor.putBoolean("firstRun", false); // It is no longer the first run
            editor.commit(); // Save all changed settings
        } else {
            Log.w("activity", "second time");
            setContentView(R.layout.activity_clean_weather);
        }
    
    }
    

    I basically took this code directly from the documentation for Storage Options and applied it to your situation. It’s a good concept to learn early.

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

Sidebar

Related Questions

Ive been trying to make a scrollable/zoomable app and everything has gone great except
I'm trying to debug an Android application with native code using ADT 20.0.3 on
Here is my code, i am trying to run in the Android. It runs
I'm trying to make an android app which has 2 layouts, one for phones
I'm trying to figure out how to detect whether a binary has been compressed
This problem has driven me crazy, it's been 5 days I'm trying to solve
I'm trying to add a UITapGestureRecognizer to my app so I can detect and
Trying to detect left click vs right click (without using jQuery!) and I have
I am trying to detect when particular applications are launched. Currently I am using
I have an issue trying to persist entities to the DB using code-first technique.

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.