I'm a beginner in mobile app development and very keen to do things the right way.
In this scenario I call an AsycTask
to populate my RecycleView
with data in the first activity.
When an item is clicked in the RecycleView
I start a second activity.
When the back button is clicked from here I go back to the first activity.
It is at this point that I need to have the same data displayed in the RecycleView
as right at the start.
How do I achieve this?
The code from MainActivity:
public class MainActivity extends AppCompatActivity {
// Variables for the search input field, and results TextViews.
private RecyclerView mRecyclerView;
private WordListAdapter mAdapter;
private LinkedList<String> mWordList = new LinkedList<>();
/**
* Initializes the activity.
*
* @param savedInstanceState The current state data
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize all the view variables.
mRecyclerView=(RecyclerView)findViewById(R.id.recyclerview);
searchBooks(new View(this));
}
/**
* Gets called when the user pushes the "Search Books" button
*
* @param view The view (Button) that was clicked.
*/
public void searchBooks(View view) {
// Get the search string from the input field.
//String queryString = mBookInput.getText().toString();
// Check the status of the network connection.
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If the network is active and the search field is not empty, start a FetchBook AsyncTask.
if (networkInfo != null && networkInfo.isConnected()) {
new FetchBook(mWordList).execute("Titanic");
try { Thread.sleep(5000); }
catch (InterruptedException ex) { android.util.Log.d("YourApplicationName", ex.toString()); }
// Create recycler view.
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
// Create an adapter and supply the data to be displayed.
mAdapter = new WordListAdapter(this, mWordList);
// Connect the adapter with the recycler view.
mRecyclerView.setAdapter(mAdapter);
// Give the recycler view a default layout manager.
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
// Otherwise update the TextView to tell the user there is no connection or no search term.
else {
}
}
}