aldoalpha
aldoalpha

Reputation: 173

Saving activity state after calling another activity through intent

I have two activities:

Activity A-->the onCreate() method interacts with a database and shows data in the Activity.

Activity B (generic)

Now,in my Activity A,I have a button that,through a Intent,brings me to Activity B;the same Intent is found in the Activity B to go back from B to A.

Here's the problem:when i go back from B to A,the onCreate method is called again,and the data is taken from the database again. Is there a way I can go from Activity B to A without having to take data again from the database,maybe 'saving' the state of the Activity A?

Thank you.

Upvotes: 0

Views: 935

Answers (3)

aldoalpha
aldoalpha

Reputation: 173

I found a solution: When Activity A calls Activity B through Intent,it passes to the Activity B the values I need to keep using putExtra.

Then the Activity B receives these values through getIntent() ,and when the Activity B calls again the Activity A through Intent,it passes back the values it received at the start.

Upvotes: 0

Ryan
Ryan

Reputation: 54

When you come back to Activity A it should be in the Resume state, so if you don't have it you should create an onResume method where that can re-handle database stuff or not. Depending on what you want when you come back to Activity A

@Override
public void onResume() {
   super.onResume();
   //saved data base stuff can be here
   System.out.println("Resuming...");   //just a sanity check if you want to watch the logs do this
}

Don't know what you're doing but you may need to separate some code where onCreate just initializes stuff then have your database interactions called elsewhere.

Upvotes: 0

iagocanalejas
iagocanalejas

Reputation: 53

If you do a new Intent its impossible to save data because u are creating a new object every time u save the method.

You have some options to solve it, try to override the onSavedInstanceState method in the activity.

You can also send the data u have do save from one activity to another.

I can help u more if u give us some code example :)

Upvotes: 1

Related Questions