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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:04:51+00:00 2026-06-11T15:04:51+00:00

I recently converted my app from an activity based app to a fragment based

  • 0

I recently converted my app from an activity based app to a fragment based app. It’s a score keeping app, and I was easily able to save and restore score when it was an activity. However, that doesn’t seem to be working as a fragment. Here’s my code:

 @Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);

    if(t!=null&&flag==1){
        savedInstanceState.putInt("time", t.getTimeLeft());
    } else {
        savedInstanceState.putInt("time", 0);
    }

    savedInstanceState.putIntArray("score_array", points);
    savedInstanceState.putIntArray("position_array", spinnerPosition);
    savedInstanceState.putBooleanArray("checked_array", shouldBeChecked);

    flag = 0;
    Log.d("MyApp", "savingState");
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    Log.d("MyApp", "onActivityCreated");

    try {
        int timeLeft = savedInstanceState.getInt("time");

        points = savedInstanceState.getIntArray("score_array").clone();
        spinnerPosition = savedInstanceState.getIntArray("position_array").clone();
        shouldBeChecked = savedInstanceState.getBooleanArray("checked_array").clone();

        ((BaseAdapter) ((ListView)getView().findViewById(R.id.missionList)).getAdapter()).notifyDataSetChanged();

        if(timeLeft!=0){
            flag=1;

            this.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            t = new TimerClass(timeLeft*1000, 1000);
            t.setViews((TextView)getView().findViewById(R.id.minuteView), (TextView)getView().findViewById(R.id.tenSecondView), (TextView)getView().findViewById(R.id.secondView), (Button)getView().findViewById(R.id.start_button));
            ((Button)getView().findViewById(R.id.start_button)).setText(R.string.stop);
            t.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 

}

I used this exact same code successfully on an activity in onRestoreInstanceState rather than onActivityCreated and without the try/catch. The problem is I’m getting null pointer errors an time I try and pull something from the bundle. I can see saving state in the log, and then onActivityCreated, but onActivityCreated doesn’t seem to be getting the stuff put in the bundle by onSavedInstanceState. I just get a null pointer on every line that calls savedInstanceState.getAnything(). However, from my reading, onCreate, onCreateView, and onActivityCreated all use the same bundle. I have tried moving the code to the other two with no luck.

  • 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-11T15:04:52+00:00Added an answer on June 11, 2026 at 3:04 pm

    I ended up coming up with a fairly hacky work around. Not thrilled with it, since it’s not actually solving the core problem. If anyone can help me find that, I’d happy accept that answer over this.

    That said, I simply used the activity’s onSaveInstanceState and onRestoreInstanceState to save and restore the fragment data.

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

Sidebar

Related Questions

We have an existing (old) codeline that we have recently converted from VC6 to
I've recently converted an app from .net 2.0 to 3.5 but I don't see
I'm currently working with a rails 3.0.x app that's recently been upgraded from rails
Ive recently get IPBoard board software for my gaming community, I converted it from
I recently converted my iPhone app to be for iPad as well by right-clicking
I have recently inherited a J2EE Struts web app that was written back in
I have a web app that uses a flash uploader (swfuploader) and recently a
Recently I've been wondering about the fact that that an iOS app only has
Recently converted my ASP.NET project from 1.1 to 3.5. Hooray! Currently developing a form
I recently converted from macports to homebrew and my previous macports Imagemagick install was

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.