Java tutorial
/* * This file is part of Droidpile: a Mailpile client for Android. * * You should have recieved a copy of the LICENSE file along with Droidpile. * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE> */ package org.maikelwever.droidpile; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NavUtils; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuItem; import org.maikelwever.droidpile.fragments.MailListFragment; /** */ public class SearchActivity extends SherlockFragmentActivity { private String searchQuery = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); MailListFragment fragment = new MailListFragment(); if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { fragment.setSearchQuery(getIntent().getStringExtra(SearchManager.QUERY)); } else { Bundle b = getIntent().getExtras(); if (b.containsKey("previousSearch")) { fragment.setSearchQuery(b.getString("previousSearch")); } } getSupportFragmentManager().beginTransaction().add(R.id.searchViewGroup, fragment).commit(); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putString("previousSearch", searchQuery); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == android.R.id.home) { NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } @Override protected void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { this.searchQuery = intent.getStringExtra(SearchManager.QUERY); doSearch(); } else if (!this.searchQuery.isEmpty()) { doSearch(); } } private void doSearch() { if (!this.searchQuery.isEmpty()) { ((MailListFragment) getSupportFragmentManager().findFragmentById(R.id.searchViewGroup)) .doSearch(this.searchQuery); } } }