Back button stopped to work in only one Activity. I have three activities which have the same parent. But one of them stopped to back (CurrentMovieActivity). I can't figure out why. Manifest seems to be correct.
What's a possible reason for it?
My Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_movie_white_24dp"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_movie_white_24dp"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activity.MovieActivity">
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/settings_title"
android:name=".activity.SettingsActivity"
android:parentActivityName=".activity.MovieActivity" />
<activity
android:label="#string/current_title"
android:name=".activity.CurrentMovieActivity"
android:parentActivityName=".activity.MovieActivity" />
<activity
android:label="#string/favourites"
android:name=".activity.FavouritesActivity"
android:parentActivityName=".activity.MovieActivity" />
<provider
android:authorities="com.globallogic.v_holodynskyi.imdbclient"
android:exported="false"
android:name=".database.MovieProvider" />
I also tried to change launch mode and to override onBackPress. It looks like this, but I can't see any logs:
#Override
public void onBackPressed() {
Log.i(LOG_TAG, "back pressed1");
super.onBackPressed();
Log.i(LOG_TAG, "back pressed2");
}
My "broken" activity:
public class CurrentMovieActivity extends AppCompatActivity {
private static final String LOG_TAG = CurrentMovieActivity.class.getSimpleName();
private ImageView mPoster;
private TextView mMovieTitle;
private TextView mMovieDescription;
private FloatingActionButton mFloatingButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_movie);
initializeVariables();
}
#Override
public void onBackPressed() {
Log.i(LOG_TAG, "back pressed1");
super.onBackPressed();
Log.i(LOG_TAG, "back pressed2");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.current_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
Intent openFavourite = new Intent(this, FavouritesActivity.class);
startActivity(openFavourite);
break;
default:
break;
}
return true;
}
private void initializeVariables() {
Intent intent = getIntent();
final String title = intent.getExtras().getString(Intent.EXTRA_TEXT + TITLE);
final String overview = intent.getExtras().getString(Intent.EXTRA_TEXT + DESCRIPTION);
final String posterLocation = intent.getExtras().getString(Intent.EXTRA_TEXT + LOCATION);
mPoster = findViewById(R.id.iv_movie_poster_current);
downloadImage(posterLocation, mPoster);
mMovieTitle = findViewById(R.id.tv_movie_title);
mMovieTitle.setText(title);
mMovieDescription = findViewById(R.id.tv_movie_description_current);
mMovieDescription.setText(overview);
mFloatingButton = findViewById(R.id.fb_add_to_favourites);
mFloatingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
saveMovie(CurrentMovieActivity.this, title, overview, posterLocation, CONTENT_URI);
}
});
}
}
This is probably because of using android:parentActivityName. When you using the attribute, it makes your code checking for android.R.id.home in onOptionsItemSelected method.
You need to check for android.R.id.home and don't use a default case in the switch like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
finish(); // call finish and don't use default case.
break;
}
return true;
}
Here is related documentation: Providing Up Navigation
check for override onBackPress() code as either you have deleted the code inside it. add finish() inside the onBackPress(),.
Related
Background
I have two activities namely MainActivity and AddPersion respectively. I also have defined a java class namely BaseActivity which implements toolbar and menu items.
I want to move from MainActivity to AddPerson activity with the help of an Intent and then back to MainActivity by clicking the back navigation. I tried it by following this video:
How to add Android Back Button
Problem
When I click on the back navigation button after moving to AddPerson nothing happens i.e I don't get moved back to MainActivity.
Code
BaseActivity
public class BaseActivity extends AppCompatActivity {
private Context context; // tried Activity context too with no luck
// code for onCreate here
// code for onCreateOptionsMenu here
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (getItemId()){
case R.id.share:
// code for share here
break;
case R.id.addPerson:
startActivity(new Intent(getContext(), AddPerson.class));
}
return true;
}
public void initToolbar(boolean isChild){
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(isChild);
}
public void setContext(Context context){ this.context = context; }
public Context getContext(){ return context; }
}
MainActivity
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar(false);
setContext(this);
}
}
AddPerson
public class AddPerson extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
initToolbar(true);
}
}
AndroidManifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".AddPerson"
android:parentActivityName=".MainActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I know the problem is with context but I don't know how to fix it.
In AddPerson Activity, you should override onOptionsItemSelected()
Using switch case, add a case for androi.R.id.home, and apply onBackPressed() .
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void initToolbar(Context from,Context to,boolean isChild){
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new
Intent(from,to)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
});
}
And if you in AddPerson activity and want to go to Main activity you can call the function like this:
initToolbar(AddPerson.this,MainActivity.class,true);
and you can change to and from parameters depend on what you want.
I was trying to intent The getSupportActionBar() to the previous page.
While creating a back arrow to toolbar , I am getting an error for the second method says error: cannot find symbol class MenuItem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override // This method creates the error
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(StartActivity.this, MainActivity.class));
}
Try This to get click event of back arraow
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// perform Your action here
return true;
}
return false;
}
and in your manifest file add parent activity to your activity like this
<activity
android:name=".yourActivity"
android:parentActivityName=".ParentActivityname"/>
You can try this
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
#Override // This method creates the error
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(StartActivity.this, MainActivity.class));
return true;
}
return false;
}
Add the meta-data tag in your AndroidManifest.xml inside tag like this
<activity
android:name=".StartActivity"
android:parentActivityName="your package name.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="your package name.MainActivity" />
</activity>
Note: Change "your package name" to your project package name
Ratilal Chopda already posted the right answer, here is only the solution in pretty code
public class ServicesViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// etc...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file
<activity android:name="com.example.ServicesViewActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
</activity>
http://developer.android.com/design/patterns/navigation.html#up-vs-back
You could also check if menu item was imported properly
that was my own experience the class wasn't imported hence it couldn't find the MenuItem
Action Bar up navigation button works fine in emulator but does not work in device can any one help me out
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
tf = Typeface.createFromAsset(getAssets(), "font/AGENCYB.TTF");
Button btnWeatherDetails = (Button) findViewById(R.id.btnWeather);
Button btnMovieDetails = (Button) findViewById(R.id.btnMovies);
Button btnHoroscopeDetails = (Button) findViewById(R.id.btnHoroscope);
btnWeatherDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, WeatherActivity.class);
MainActivity.this.startActivity(i);
}
});
btnMovieDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, MovieActivity.class);
MainActivity.this.startActivity(i);
}
});
btnHoroscopeDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, HoroscopeActivity.class);
MainActivity.this.startActivity(i);
}
});
}
#Override
protected void onDestroy() {
SharedPreferences settingsPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
SharedPreferences.Editor settingsEditor= settingsPreferences.edit();
settingsEditor.putBoolean(MovieActivity.MOVIEUPDATED, false);
settingsEditor.putBoolean(HoroscopeActivity.HOROSCOPEAVAILABLE, false);
settingsEditor.commit();
super.onDestroy();
}
WeatherActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollmain);
getActionBar().setDisplayHomeAsUpEnabled(true);
settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE);
SavedString = settingsPreferences.getString(JSONWEATHERSTRING, "Not Found");
new JSONParse().execute(WeatherUrl);
}
public boolean onCreateOptionMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item)
{
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed()
{
moveTaskToBack(true);
WeatherActivity.this.finish();
}
MovieActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE);
MovieJsonString = settingsPreferences.getString(JSONMOVIESTRING, "Not Found");
isMovieUpdated = settingsPreferences.getBoolean(MOVIEUPDATED, false);
setContentView(R.layout.main_list_view);
getActionBar().setDisplayHomeAsUpEnabled(true);
movieList = new ArrayList<HashMap<String, String>>();
list = getListView();
// Calling async task to get json
if(!isMovieUpdated)
new GetMovieDetails().execute();
else
GetMoviePreferences();
}
public boolean onCreateOptionMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item)
{
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed()
{
moveTaskToBack(true);
MovieActivity.this.finish();
}
HoroscopeActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horoscope);
getActionBar().setDisplayHomeAsUpEnabled(true);
settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE);
isHoroAvailable = settingsPreferences.getBoolean(HOROSCOPEAVAILABLE,false);
if(!isHoroAvailable)
new XMLParse().execute();
else
GetHoroscopeFromPreference();
}
public boolean onCreateOptionMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item)
{
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed()
{
moveTaskToBack(true);
HoroscopeActivity.this.finish();
}
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.datumdata.weather"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_hosurdata"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="in.datumdata.hosurdata.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="in.datumdata.hosurdata.MovieActivity"
android:label="Movie Details"
android:parentActivityName="in.datumdata.hosurdata.MainActvity"
android:screenOrientation="portrait" />
<activity
android:name="in.datumdata.hosurdata.WeatherActivity"
android:label="Weather Details"
android:parentActivityName="in.datumdata.hosurdata.MainActvity"
android:screenOrientation="portrait" />
<activity
android:name="in.datumdata.hosurdata.SingleContactActivity"
android:label="Movie Details"
android:parentActivityName="in.datumdata.hosurdata.MovieActivity"
android:screenOrientation="portrait" />
<activity
android:name="in.datumdata.hosurdata.HoroscopeActivity"
android:label="Horoscope Details"
android:parentActivityName="in.datumdata.hosurdata.MainActvity"
android:screenOrientation="portrait" />
<activity
android:name="in.datumdata.hosurdata.SingleHoroscopeActivity"
android:label="Horoscope Details"
android:parentActivityName="in.datumdata.hosurdata.HoroscopeActivity"
android:screenOrientation="portrait" />
</application>
</manifest>
In all the above activities the navigation button works fine in emulator but on device it does not work and when i press the back button on the device the application goes invisible i mean to say its running in the background. This thing happens on all activities on whatever activity i press back button the same thing happens
help me out. thanks in advance and sorry for my bad english.
You should use a meta data tags for Up button to work on older APIs:
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
EDIT:
You already have the parentActivityName, so add only the meta tag to your AndroidManifest.xml file's tags. Like so:
<activity
android:name="in.datumdata.hosurdata.MovieActivity"
android:label="Movie Details"
android:parentActivityName="in.datumdata.hosurdata.MainActvity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="in.datumdata.hosurdata.MainActvity" />
</activity>
Where you do this:
getActionBar().setDisplayHomeAsUpEnabled(true);
Try doing this:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
If your activity gets called from multiple activities and specifying parent activity doesn't fit your use case, then you can fix it by adding this:
getSupportActionBar().setDisplayHomeAsUpEnabled(true); (in onCreate)
#Override
public boolean onNavigateUp() {
finish();
return true;
}
#Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
I had problems with support action bar and if you're using support actionbar then you've to add this line in your code.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And in the manifest you need to add
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
So the activity declaration in manifest will look like:
<activity
android:name="com.example.MyActivity"
android:label="My Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
</activity>
You can't add android:parentActivityName="com.example.ParentActivity" in the activity declaration in manifest if you're using API level less than 16.
I have created an activity named Blocks, but when I open it it automatically returns to Home activity which is the main activity of the program.
Here is the code
Blocks.class
public class Blocks extends FragmentActivity implements ActionBar.OnNavigationListener {
/**
* The serialization (saved instance state) Bundle key representing the
* current dropdown position.
*/
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blocks);
ImageView img_blocks = (ImageView) findViewById(R.id.map);
img_blocks.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Rumaithiya+City"));
startActivity(myIntent);
}
});
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(
actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
}),
this);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
getActionBar().getSelectedNavigationIndex());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blocks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menumap:
Intent myIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:29.316097,48.071147?z=14"));
startActivity(myIntent);
break;
case R.id.menuwiki:
String wiki = "http://j.mp/rumwiki";
Intent w = new Intent(Intent.ACTION_VIEW);
w.setData(Uri.parse(wiki));
startActivity(w);
break;
default:
break;
}
return true;
}
#Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
switch (position) {
case 0:
startActivity(new Intent(this, Home.class));
return true;
case 1:
startActivity(new Intent(this, Landmarks.class));
return true;
case 2:
startActivity(new Intent(this, Blocks.class));
return true;
case 3:
startActivity(new Intent(this, Numbers.class));
return true;
default:
break;
}
return true;
}
}
the problem occurred when I put the code for the spinner navigation, I can go back to "Blocks" simply by pressing the back button (and it'll not go to "Home" again).
Edit: Here is my Manifest code as requested
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rum.city"
android:versionCode="10"
android:versionName="3.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.rum.city.Home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.rum.city.Landmarks" />
<activity
android:name="com.rum.city.Blocks"
android:label="#string/title_activity_blocks" >
</activity>
<activity
android:name="com.rum.city.Numbers"
android:label="#string/title_activity_numbers" >
</activity>
<activity
android:name="com.rum.city.Hwl"
android:label="#string/title_activity_hwl"
android:theme="#android:style/Theme.Holo.Light.Dialog" >
</activity>
</application>
Edit: As the other users said, the savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM) returns 0, to be sure, I changed my code to:
switch (position) {
case 0:
return true;
And the program didn't go to Home, but the problem is this way the user won't be able to use the spinner to choose Home activity because it'll do nothing :(
When you set navigation callback it fires immediately and Home activity starts because it goes first. For exmaple try to set flag initialized and check it in onNavigationItemSelected.
private boolean mInitialized = false;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar.setListNavigationCallbacks(adapter,
this);
mInitialized = true;
}
#Override
public boolean onNavigationItemSelected(int position, long id) {
if (!mInitialized) {
return false;
}
return true;
}
Am using AndroidAnnotations with SherlockActionBar and am trying to implement a search from the MainActivity to a SecondActivity. In the SecondActivity I want to be able to search again. However, if I click on the search widget in the SecondActivity it returns to the first activity. It works fine without the search widget in the SecondActivity.
Thank you in advance!
MainActivity:
#EActivity
#OptionsMenu(R.menu.main)
public class MainActivity extends TabSwipeActivity {
SearchView sv;
#SystemService
SearchManager searchManager;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
SearchShowsActivity:
#EActivity(R.layout.activity_search_shows)
#OptionsMenu(R.menu.search_menu)
public class SearchShowsActivity extends SherlockActivity {
SearchView sv;
#SystemService
SearchManager searchManager;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
// perform search
}
}
main.xml (menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/search_shows"
android:title="#string/search_label"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
AndroidManifest.xml
<meta-data
android:name="android.app.default_searchable"
android:value=".activities.SearchShowsActivity_" />
<activity
android:label="#string/app_name"
android:name=".activities.MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop"
android:name=".activities.SearchShowsActivity_">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
Fixed this by starting the SearchShowsActivity_ when clicking on the search icon in the MainActivity_:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.add_show) {
SearchShowsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP).start();
return true;
}
return false;
}
In my SearchShowsActivity_ I start the SearchView:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
sv.setFocusable(true);
sv.setIconified(false);
sv.requestFocusFromTouch();
return true;
}