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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:45:27+00:00 2026-06-10T17:45:27+00:00

I am experimenting with using fragments in my application. I am trying to find

  • 0

I am experimenting with using fragments in my application. I am trying to find a way to store data used by a fragment when I switch my application to the other fragment. My code for my second fragment is found below. When switching to and from this fragment I am using add and remove fragment. This is because when I was using replace, I was having trouble. Whenever I switch to my other fragment, and back to this one, count is not stored. How do I actually store count so I can come back to it when my fragment opens back up?

public class BasicFragment2 extends Fragment {

public int count = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null){
        count = savedInstanceState.getInt("Integer");
    }

    View view = inflater.inflate(R.layout.fragment_basic_2, container, false);

    Button button = (Button) view.findViewById(R.id.button2);

    // A simple OnClickListener for our button. You can see here how a Fragment can encapsulate
    // logic and views to build out re-usable Activity components.
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Activity activity = getActivity();
            count++;

            if (activity != null) {
                Toast.makeText(activity, "This is not a fragment...Yes it is " + count, Toast.LENGTH_LONG).show();
            }
        }

    });

    return view;
}

public void onSaveInstanceState(Bundle outState){
    outState.putInt("Integer",count);
    super.onSaveInstanceState(outState);
}

}
  • 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-10T17:45:28+00:00Added an answer on June 10, 2026 at 5:45 pm

    Try overriding onSaveInstanceState in your Activity class and save the count value there. Because as per google docs:

    Note however: this method may be called at any time before
    onDestroy(). There are many situations where a fragment may be mostly
    torn down (such as when placed on the back stack with no UI showing),
    but its state will not be saved until its owning activity actually
    needs to save its state.

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

Sidebar

Related Questions

I'm currently experimenting with using WCF Data Services as a way to open up
We use SVN for our source-code revision control and are experimenting using it for
I've been experimenting with using MS Excel 2007 to edit tabular data stored in
i am experimenting using Hibernate. I am trying to get a User by id
I'm experimenting with using couchdb as a message store and would like to report
I'm experimenting with using a string for storing different kind of data types in
I have a multi-tenant application and I'm experimenting with using the i18n gem to
I'm experimenting with using the pg_bulkload project to import millions of rows of data
I'm experimenting ideas around using AppDomain to manage some legacy code contains lots of
Experimenting using different approaches and reducing code maintenance I have ended up using reflection

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.