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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:17:01+00:00 2026-06-15T13:17:01+00:00

I am trying to save some data in MainActivity when switching to another activity,

  • 0

I am trying to save some data in “MainActivity” when switching to another activity, and restoring that data as I switch back to it.

In “MainActivity”: (restoring data)

            protected void onCreate(Bundle savedInstanceState) {
                   // do usual stuff
                   restoreData();
            }

In “MainActivity”: (switching to “StatusActivity”):

            Bundle data = saveData();
            Log.d(TAG, "Sending data to status activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
            intent.putExtras(data);
            startActivity(intent);  

In “StatusActivity”:

            Bundle data = getIntent().getExtras();
            Log.d(TAG, "Sending data to main activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtras(data);
            startActivity(intent);  

My saveData() function:

private Bundle saveData() {
    Log.d(TAG, "Started saving state");
    Bundle data = new Bundle();

    // store stuff in the bundle
    return data;
}

My restoreData() function:

private void restoreData() {
    Log.d(TAG, "Started restoring state");

    Bundle data = getIntent().getExtras();
            // restore stuff in the bundle
}

LogCat:

My bundle is fine when sending to StatusActivity:

Sending data to status activity intent: 
Bundle[{obj0=Bundle[{timeSinceLastPooped=3224, hunger=5, 
id=2130837505, timeSinceLastHungerUpdate=3224, 
timeSinceLastFed=0, timeSinceLastHappinessUpdate=3224, 
timeSinceLastEvolution=3224, posY=0.0, posX=0.0, 
isDead=false, happiness=5, evolutionStage=0, type=pet}], 
time=7.794168E7}]

But then sending back to MainActivity:

Sending data to main activity intent: Bundle[mParcelledData.dataSize=648]

What do I do with mParcelledData to get the original bundle back? Thanks!

Answer:

In “MainActivity”: (restoring data)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if(requestCode == 1) {
        if(resultCode == RESULT_OK) {
            restoreData(intent);
        }
    }
}

In “MainActivity”: (switching to “StatusActivity”):

            Bundle data = saveData();
            Log.d(TAG, "Sending data to status activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
            intent.putExtras(data);
            startActivityForResult(intent, 1);

In “StatusActivity”:

            Bundle data = getIntent().getExtras();
            Log.d(TAG, "Sending data to main activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtras(data);
            setResult(RESULT_OK, getIntent());
            finish();   
  • 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-15T13:17:02+00:00Added an answer on June 15, 2026 at 1:17 pm

    You can use onRestoreInstanceState() witch is called after onStart(), whereas onCreate() is called before onStart().

    Use the put methods to store values in onSaveInstanceState():

    protected void onSaveInstanceState(Bundle bundle) {
      super.onSaveInstanceState(bundle);
      bundle.putInt("value", value);
    }
    

    And restore the value in onCreate():

    public void onCreate(Bundle bundle) {
      if (bundle!= null){
        value = bundle.getInt("value");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to save some data and call it back in a tableview from
I'm trying to save some data into an array but unfortunately all the data
I'm trying to save some data to A file in android, I'm using herefor
I am trying to save some data for a android game, but I cant
I'm trying to save some lookup table data out to a YAML file so
I'm trying to save some specific JSon data and store them in a simple
I'm trying to create core data application. Some times when trying to save data,
I'm trying to save some layouts from DevExpress Grid Controls so that users can
I'm trying to catch WM_QUERYENDSESSION to save some data in the app, but it
I'm trying to write a sync function that saves some data to nodes, which

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.