I have an android search in action bar (just standard one). The way I made it working is, I created SearchQueryRouter
Activity, which receives search query and starts the MainActivity
- where all my Fragments
reside replacing each other. Everything works perfect so far. BUT once I hit android back button it redirects "back" (sarcasm) to blank activity or fragment or whatever so that Nothing is Logged (I log messages once onCreate
executes).
Any idea on how to support back button functionality properly? - I expect back button to push me either to previous search or to the default results on the Fragment.
This is my MainActivity
(Mother of Fragments)
Intent intent = getIntent();
Bundle bundle = getExtras();
if (bundle != null) {
MyFragment newFrag = new MyFragment();
Bundle args = new Bundle();
args.putString("searchQuery", bundle.getString("searchQuery"));
newFrag.setArguments(args);
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.container, newFrag);
transaction.addToBackStack(null);
transaction.commit();
}
Here is SearchQueryRouter
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Intent newIntent = new Intent();
newIntent.setClass(getApplicationContext(), MainActivity.class);
newIntent.putExtra("searchQuery", query);
finish();
}