Android Action Bar navigation up button not working on devices - android

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.

Related

Back arrow - error: cannot find symbol class MenuItem

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

Back button stopped to work in only one Activity

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(),.

Nothing happens when I press enter for query in search widget in action bar

I am making an app like password keeper. My update button is working, my view button is working and giving all the database. I am able to type my query in the search widget in the action bar but when I press enter nothing happens.... I need help, I have tried every thing.... Below is the code
MainActivity.java // in which I want to type query in Search widget
public class MainActivity extends Activity implements OnClickListener {
Button sqlUpdate, sqlView, sqlDelete;
EditText sqlWebsite, sqlUser, sqlPass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlUpdate = (Button) findViewById(R.id.update_button);
sqlView = (Button) findViewById(R.id.view_button);
sqlWebsite = (EditText) findViewById(R.id.websiteText);
sqlUser = (EditText) findViewById(R.id.userText);
sqlPass = (EditText) findViewById(R.id.passText);
sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
sqlView.setOnClickListener((android.view.View.OnClickListener) this);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
//#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public void onClick(View arg0) {
switch (arg0.getId())
{
case R.id.update_button:
boolean didItWork = true;
try{
String website = sqlWebsite.getText().toString();
String username = sqlUser.getText().toString();
String password = sqlPass.getText().toString();
UpdateActivity entry = new UpdateActivity(MainActivity.this);
entry.open();
entry.createEntry(website, username, password);
entry.close();
}catch (Exception e){
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Failed!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Done!");
TextView tv = new TextView(this);
tv.setText("Successfull");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.view_button:
Class c = null;
try {
c=Class.forName("com.example.store_passwords.ViewActivity");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent(MainActivity.this,c);
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Searchable.java // in which opens after intent is passed in the manifest file. I did implement the list view but then I tried a table view for searchableActivity... Well that is not the problem
public class SearchableActivity extends Activity {
//TextView selection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.searchable, menu);
return true;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Toast.makeText(this, "Entered handle intent: ", Toast.LENGTH_SHORT).show();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
doMySearch(query);
Toast.makeText(this, "QUERY after do my search: " + query, Toast.LENGTH_SHORT).show();
}
}
private void doMySearch(String query) {
// TODO Auto-generated method stub
TextView tv1 = (TextView) findViewById(R.id.tvSQLinfo4);
UpdateActivity info1 = new UpdateActivity(this);
info1.open();
String data1 = info1.getWebsitesSearch(query);
info1.close();
tv1.setText(data1);
TextView tv2 = (TextView) findViewById(R.id.tvSQLinfo5);
UpdateActivity info2 = new UpdateActivity(this);
info2.open();
String data2 = info2.getUsernamesSearch(query);
info2.close();
tv2.setText(data2);
TextView tv3 = (TextView) findViewById(R.id.tvSQLinfo6);
UpdateActivity info3 = new UpdateActivity(this);
info3.open();
String data3 = info3.getPasswordsSearch(query);
info3.close();
tv3.setText(data3);
}
}
re/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_launcher"
android:title="Search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
searchable.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.andorid.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/search_here"/>
AndroidManifest.xml // I have also tried putting the meta-data in the main activity in this file which didn't work so I put it inside the application tag
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.store_passwords"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity
android:name="com.example.store_passwords.MainActivity"
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.example.store_passwords.UpdateActivity"
android:label="#string/title_activity_update" >
</activity>
<activity
android:name="com.example.store_passwords.ViewActivity"
android:label="#string/title_activity_view" >
</activity>
<activity android:name=".SearchableActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>

My activity returns to "Home" Activity when opened

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;
}

ActionBarSherlock Search widget returns to FirstActivity from SecondActivity

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;
}

Categories

Resources