I have an activity that perform a search of movies and parse the JSON results into "Movie" Objects.
after the parsing the Movie objects are being inserted into a ListView Adapter.
When i change oriantation the activity restarts and the results are gone.
How can i prevent this from happen or save the Objects and restore them onRestoreInstanceState?
I have already tried onConfigurationChanged.
You need to do a little bit of refactoring.
First implement onSaveInstanceState and save your list to a bundle
#Override
protected void onSaveInstanceState (Bundle outState){
outState.putSerializable("mylist", mylist);
}
Then on your OnCreate check if your bundle has the list and opulate your listview without fetching again your list
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(savedInstanceState!=null && savedInstanceState.getSerializable("mylist")!=null){
populateListView(savedInstanceState.getSerializable("mylist"));
}else{
fetchList();
}
}
Related
I have an Activity with a Fragment inside.
In that fragment there are RecyclerView, and RecyclerViewAdapter.
In fragment's onCreate I'm doing this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TAGGG", "onCreate: ");
// Database handler
Contract.DbHelper dbHelper = new Contract.DbHelper(getContext());
dERecyclerViewAdapter.setEntries(dbHelper.readAllEntries());
}
Everytime the Activity is recreated(up navigation, rotation changes) data is being read again from the database.
What is the correct way to store it?
You can use the savedInstanceState to store the data through a bundle. You can store it like this
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Use the bundle to store data
}
And retrieve it like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null)
{
//Use the savedInstanceState to get the stored result
}
}
Hope this helps !
You should look at the CursorLoader class. Your situation is exactly what this class was intended for.
The loader itself can persist between configuration changes, so that when you call onCreate, if the loader has already performed the query it will just provide the data it already read rather than re-query the database.
See https://developer.android.com/guide/components/loaders.html
Also http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html
I'm wondering if there is a way to restore, after rotation, my list of results without retaining the fragment.
Basically I have a fragment which calls, through a presenter, some api (using RxJava and Retrofit). I added pagination so I can make a call only when I need more data scrolling down.
I'm in the following scenario:
- I scroll the list down in order to call the second page from the web
- I rotate the screen
In this case what I would need is to show all the items, from page 1 and 2, and then scrolling to the correct position (for this I can use the onSaveInstanceState of the LayoutManager).
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FwApplication.component(getActivity()).inject(this);
if (savedInstanceState != null) {
mRxRunning = savedInstanceState.getBoolean(EXTRA_RX);
mQuery = savedInstanceState.getString(QUERY_ARG);
mCurrentPage = savedInstanceState.getInt(CURRENT_PAGE);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(QUERY_ARG, mQuery);
outState.putInt(CURRENT_PAGE, mCurrentPage);
outState.putParcelable(LAYOUT_STATE, mRecyclerView.getLayoutManager().onSaveInstanceState());
super.onSaveInstanceState(outState);
}
#Override
public void onResume() {
super.onResume();
mPresenter.onResume(mRxRunning, mCurrentPage);
}
Is there a way to save the items without calling setRetainInstance? Moreover I would avoid to call the api in order to get all the items back.
The items are POJOs so it won't be a list of simple strings.
Try to edit the Manifest file. Add this line to block with activity:
android:configChanges="keyboardHidden|orientation|screenSize">
It will prevent Activity restart on such events (specially on orientation change)
In my OnCreate method I am making a network call and populating a listview with the results. I am saving the result in an ArrayList and using the ArrayList to populate the listview. If the user presses the back button and reenters the activity I would like to store the ArrayLists in a SavedInstanceState Bundle and populate the ListView with the stored ArrayLists instead of making the network call a second time. The following is my OnCreate code:
if (savedInstanceState != null) {
largeImage = savedInstanceState.getParcelableArrayList("Image");
flowercode = savedInstanceState.getStringArrayList("Code");
flowerprice = savedInstanceState.getStringArrayList("Price");
flowerdimensions = savedInstanceState.getStringArrayList("Dimension");
largeImageText = savedInstanceState.getStringArrayList("Imagetext");
ListView listView = (ListView) findViewById(R.id.LowRomance);
FlowerShopAdapter listAdapter = new FlowerShopAdapter(this, flowercode, flowerName, flowerprice, largeImage, flowerdimensions);
listView.setAdapter(listAdapter);
} else if(savedInstanceState == null) {
makesoapcall();
}
}
This is my OnSaveInstanceState code
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putParcelableArrayList("Image", largeImage);
savedInstanceState.putStringArrayList("Code", flowercode);
savedInstanceState.putStringArrayList("Price", flowerprice);
savedInstanceState.putStringArrayList("Dimension", flowerdimensions);
super.onSaveInstanceState(savedInstanceState);
}
Currently my App is making the network call every single time. Is their something else I need to implement in order to get this to work or is their an error in the current way I am trying to implement this? The ArrayList's are definitely not null when I put them into savedInstanceState.
onSaveInstanceState() is not supposed to be called when the back button is pressed on an Activity. It's called when Android decides to destroy the Activity.
e.g. When orientation changes.
What you need here is SharedPreferences. Store the ArrayLists using SharedPreferences in onStop(). And get it in onStart(). Here is how to store ArrayList in SharedPreferences.
I've got a ListView, an ArrayList. From activity1, I pass some data, e.g a price and a name of a company. This data will be stored in an ArrayList of Hashmaps. When I first passes some data to the second activity which holds the ListView the data will be found in this list, but when I go back to the first activity and pass some information again, the old data will still be in the list. Is there any possible way I can prevent this to happend? For instance, when I return from activity2 to activity1, the ListView will be wiped.
Thanks!
what you need to do:
on the activity which holds the listView - override the onResume() method:
#Override
protected void onResume() {
super.onResume();
mArrayList.clear();
mAdapter.notifyDataSetChanged();
}
if it's not good for you to clear the array list, so you can do the following:
#Override
protected void onResume() {
super.onResume();
mListView.removeAllViews();
}
I am using a cursor adapter to retrieve the data from the database and binding it to the spinner control.
It's working fine but the problem occurs when user sending the application in the background after that all the spinner control looses the data.
Help me,
Waiting for your valuable answers.
ya it will happen, for that you have to save object Instance using below methods 0f Activity
#Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
// retrieve stored data
savedState.get[...]
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// and place it
outState.put[...]
}