I have a database of >100 objects. Each object has a specific image filename, subject string, title string and pdf filename.
I have an activity that is a grid view of objects.
When first created the grid view will load an array of objects to display. The exact content of the array can vary depending on the database query I run when first opening the activity.
The default setting is that all objects from the database are retrieved and shown. However I have times when only a subset of objects (based on subject) are retrieved and shown.
Clicking on a button in the grid opens an activity which shows the pdf of that object. Fine.
The issue is, when I 'go back' from the PDF activity, the grid view is recreated. Always with the 'default' all objects.
Thus if I have a grid view showing objects of only a certain 'subject', if I then open the pdf activity, upon returning the grid view is recreated and shows all objects, not the original grid view before the pdf activity was started.
Question: 1) How do I return to the grid view WITHOUT it being recreated?
Code:
public class SubjectListActivity extends AppCompatActivity {
String DB_NAME = "XXXXX.sqlite";
String TABLE_NAME = "XXXXX";
DataBaseHelper myDBHelper;
private GridView gridView;
private MyAdapter myAdapter;
private ArrayList<Subject> subjectsArrayList;
private Context context;
private String subjectAreas = ""; // this is sent to the activity from whichever activity opened it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_list);
Bundle extras = getIntent().getExtras();
if (extras != null) {
subjectAreas = extras.getString("subjectAreas");
}
gridView = findViewById(R.id.gv);
// Database
AssetDatabaseOpenHelper assetDatabaseOpenHelper = new AssetDatabaseOpenHelper(this, DB_NAME);
assetDatabaseOpenHelper.saveDatabase();
myDBHelper = new DataBaseHelper(this, DB_NAME);
// populates subjectsArray
subjectsArrayList = populateSubjects();
loadGrid();
// listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String title = subjectsArrayList.get(position).getSubjectTitle();
String iconStyle = subjectsArrayList.get(position).getSubjectIconStyle();
String pdf = subjectsArrayList.get(position).getSubjectPDF();
String ref = subjectsArrayList.get(position).getSubjectRefs();
String linked = subjectsArrayList.get(position).getSubjectLinked();
Intent intent = new Intent(view.getContext(), SubjectActivity.class);
intent.putExtra("title", title);
intent.putExtra("icon", iconStyle);
intent.putExtra("pdf", pdf);
intent.putExtra("ref", ref);
intent.putExtra("linked", linked);
intent.putExtra("subjectAreas", subjectAreas);
startActivityForResult(intent, 1);
}
});
}
private void loadGrid(){
myAdapter = new MyAdapter(getApplicationContext(), subjectsArrayList);
gridView.setAdapter(myAdapter);
}
// The SQL query that populates the subjectArray depends on the 'subjectAreas' string which is set when the activity first opens. The default is 'getAllSubjects'
private ArrayList<Subject> populateSubjects() {
Cursor res = myDBHelper.getAllSubjects(TABLE_NAME);
if (subjectAreas.equals("emerg")){res = myDBHelper.getEmergSubjects(TABLE_NAME);}
else if (subjectAreas.equals("all")){res = myDBHelper.getAllSubjects(TABLE_NAME);}
else if (subjectAreas.equals("ax")){res = myDBHelper.getAxSubjects(TABLE_NAME);}
else if (subjectAreas.equals("mx")){res = myDBHelper.getMxSubjects(TABLE_NAME);}
else if (subjectAreas.equals("ref")){res = myDBHelper.getRefSubjects(TABLE_NAME);}
else if (subjectAreas.equals("cal")){res = myDBHelper.getCalSubjects(TABLE_NAME);}
else if (subjectAreas.equals("search")){res = myDBHelper.getSearchSubjects(TABLE_NAME, keywords);}
ArrayList<Subject> list = new ArrayList<>();
if (res.getCount() == 0) {
} else {
while (res.moveToNext()) {
Subject subject = new Subject();
subject.setSubjectID(res.getString(0));
subject.setSubjectTitle(res.getString(1));
subject.setSubjectIconStyle(res.getString(2));
subject.setSubjectPDF(res.getString(3));
subject.setSubjectKeywords(res.getString(4));
subject.setSubjectLinked(res.getString(5));
subject.setSubjectRefs(res.getString(6));
list.add(subject);
}
}
return list;
}
// Tried sending 'back' subjectAreas from PDF activity and re-populating the array and gridview - this doesn't seem to be called
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
subjectAreas = data.getStringExtra("subjectAreas");
subjectsArrayList = populateSubjects();
myAdapter = new MyAdapter(getApplicationContext(), subjectsArrayList);
gridView.invalidateViews();
gridView.setAdapter(myAdapter);
}
}
}
}
Thanks in advance
UPDATE: Using savedInstanceState to 'save' my subjectAreas string I've used the following:
private String subjectAreas = "";
private static final String subjectAreasSaved = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_list);
Bundle extras = getIntent().getExtras();
if (extras != null) {
subjectAreas = extras.getString("subjectAreas");
title = extras.getString("title");
keywords = extras.getStringArrayList("keywords");
}
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
subjectAreas = savedInstanceState.getString(subjectAreasSaved);
}
And:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(subjectAreasSaved, subjectAreas);
super.onSaveInstanceState(savedInstanceState);
}
Is the onSaveINstanceState called automatically when the second activity is open-end or do I need to call this? At present nothing appears to be happening