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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:30:30+00:00 2026-06-11T09:30:30+00:00

private static final String KEY = qaz; private String aString; @Override public void onCreate(Bundle

  • 0
private static final String KEY = "qaz";
private String aString;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.txt);
    final String saved;
    if (savedInstanceState != null) {
        saved = savedInstanceState.getString(KEY);
        report("[" + saved + "]");
    } else {
        saved = null;
        report("[NULL]");
    }
    if (aString==null && saved!=null) {
        aString = saved;
        report("A");
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    report("s");
    savedInstanceState.putString(KEY, aString);
    super.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onPause() {
    report("p");
    super.onPause();
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private void report(String s) {
    aString += " " + s;
    Log.v("report", aString);
    textView.setText(aString);
}
  • 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-11T09:30:32+00:00Added an answer on June 11, 2026 at 9:30 am

    onSaveInstance is intended to persist a value from one instance of your activity to the next, for example when the activity is destroyed and recreated on an orientation change. Just remember that when your activity is recreated, it is an entirely new instance of the class you’ve defined to extend the Activity class and therefore, all local fields (variables) will be re-initialised.

    As you’ve already discovered, SharedPreferences are a good way to persist a value beyond the Activity lifecycle and of course, there are others. The values saved in onSaveInstance and retrieved in onCreate are intended for you to be able to initialise a new instance of your activity to the same state it was before it was destroyed.

    Imagine for example that you have a messaging application and your user has already entered several lines of text. They then accidentally flip the orientation (we’ve all done that right?) then flip it back. How frustrated would they be to see that their text had disappeared?! So a good developer would save the current content of the TextView in onSaveInstance and retrieve it in onCreate. If any value is retrieved, it is passed to the setText() method so that the user can continue from where they were.

    Take a look here. Learning the Activity life cycle, how to control it and how to pass values between Activities (either 2 different Activities or 2 instances of the same Activity) is one of the keys to unlocking the Android kingdom.

    http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    The key point is to understand when each callback is called and, even more importantly, what may or may not be called by Android. For example, when your app is in the background,there are NO guarantees that your activity will be called again if Android decides to kill your app.

    Good luck.

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

Sidebar

Related Questions

This is the calling file: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbActivities
I have the following code public class Test{ private static final String key =
I created a table without a primary key Like this: private static final String
Let's say I have: public class Components<T> extends TupleList<Class<T>, String> { private static final
Should I create private static final String = Some exception message or leave it
I have this regex: private static final String SPACE_PATH_REGEX =[a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+; I check if my
private static class FilterByStringContains implements Predicate<String> { private String filterString; private FilterByStringContains(final String filterString)
I have the following code: private static final Set<String> allowedParameters; static { Set<String> tmpSet
I found a code where it declared code like private final static String API_RTN_SUCCESS
my droDownBox look like: add(new DropDownChoice<String>(hladaneSlovo, new HladaneSlova()).add(new AjaxEventBehavior(onchange) { private static final long

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.