Reputation: 549
is there a way to prevent activity to be reloaded when navigate back to it for exemple , in case of some activitites who load data from database , it's not a good for users to wait loading data everytime they navigate to this activity.
Upvotes: 0
Views: 91
Reputation: 869
You might want to study the Activity Lifecycle in particular Recreating an Activity
You can save any data that you've gathered or created after displaying the activity for the first time by storing it in a Bundle onSaveInstanceState(Bundle ) method and then recovering it from the same Bundle
using the onRestoreInstanceState(Bundle) method.
Upvotes: 0
Reputation: 894
While invoking the activity set the intent type you want to use. Here is the documentation
For your case FLAG_ACTIVITY_REORDER_TO_FRONT may help.
But on reading your question again, I think you want to keep the data as it is and don't want to load it again. In that case, extend the Application class. Save the data in a variable in this extended class and use it gain from anywhere you want to.
Upvotes: 0
Reputation: 7108
Short answer regarding the possibility of preventing reload of Activity: no. You should always , at least in my opinion, design your app to handle the Activity lifecycle. The reload (onPause->onDestroy->onCreate->onResume) will also happen when the orientation of the device changes. Having a CPU and/or memory intensive task happen every time the Activity is created would most likely lead to poor user experience.
One solution could be to have an external class handle the database loading and let the class be accessible as a singleton. I know this is discourage by many developers but at the same time I am pretty certain to have read that it is acceptable in many Android specific cases.
Alternatively you could have a class extend Application. Then the Application class could hold on to any session relevant data. This would also give access to the data independent of which Activity is currently shown.
I am sure there is a lot of other options but this is what initially came to mind.
Upvotes: 1