App slow down when I click on options menu - android

I am working on and Android app, the main page has map fragment and some animations, when I click on options menu everything gets slow and paused for a second before the menu show up, and when I select an item from the menu same thing happens for a second until changes committed.
I use the menu to change Map type (standard, Satellite, Terrain)
Can I add a loading or progress bar before and after calling the menu options method everytime onOptionsItemSelected?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.first:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.second:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
}
return super.onOptionsItemSelected(item);
}

Try:
1. Remove MapStyleOptions styleOptions;
2. Clear map before changing type
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Add menu handling code
switch (id) {
case R.id.first:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.second:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
}
return super.onOptionsItemSelected(item);
}

Related

How to return to previously opened tab on back button press

I have a TabLayout with 3 sections. Each section contains list of items.
When I click item on second section, I start new activity like this.
Intent intent = new Intent(getContext(), SecondDetailActivity.class);
startActivity(intent);
Inside of SecondDetailActivity I have an action bar with back button.
And I handle click of back button like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
}
}
return super.onOptionsItemSelected(item);
}
My problem is:
When I click device's back button, the selected tab of the tablayout is Second tab. But when I click on action bar navigation button, the selected tab is First tab.
How can I make action bar's behavior same as device's back button?
Try to return true while your case is android.R.id.home
like
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}

Back button as Menu in fragment

I want to add a back button as menu in the left of the action bar in fragment. But I don't want the back arrow as my icon.
actionBar.setDisplayHomeAsUpEnabled(true);
The above code line gives the back arrow symbol. Instead i want to use some custom image. Also using that custom image should get it back to its previous activity.
I added something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It worked for me.
Add this to your theme in styles.xml:
<item name="android:homeAsUpIndicator">#drawable/my_icon</item>
It will override the default icon. By default it uses the following id:
android.R.id.home
You can use this id to navigate back to your previous activity:
View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
homeView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Go back
}
});
or
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Return back to your activity!
return true;
}
return super.onOptionsItemSelected(item);
}
For more information check the official documentation at : https://developer.android.com/training/implementing-navigation/ancestral.html

onOptionsItemSelected of fragment inside ViewPager not called

I have a problem with developing an app for both tablets and smartphones with fragments.
When app is executed on smartphone, I show a fragment with a list (categories). When clicking on an item, I start a new activity that holds a ViewPager that inflates Fragments with another list (detail).
When app is executed on tablet I have a layout with two fragments inside. The left one is the categories list and the right one is the detail list.
Up to here there's no problem, but when setting onOptionsItemSelected I have problems.
When executing app on smartphone, everything is working well. This works:
Activity containing ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icShare:
sharePortal();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Fragment inflated by ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icFavourite:
Utils.setFavouritePortal(mContext, mPortal);
getActivity().invalidateOptionsMenu();
return true;
case R.id.icShowFavourites:
showFavouriteArticles();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
But when executing the app in tablet, onOptionsItemSelected is not called. This is the code:
Fragment containing ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icShowFavourites:
Log.e("asdf", "asdfasdf - test tablet");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Fragment inflated by ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icFavourite:
Utils.setFavouritePortal(mContext, mPortal);
getActivity().invalidateOptionsMenu();
return true;
case R.id.icShowFavourites:
showFavouriteArticles();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
The only difference is that when executing on smartphone, the first onOptionsItemSelected is hold by an Activity and in tabled is hold by a fragment. I also tried to execute the onOptionsItemSelected from the Activity containing the fragment that contains the ViewPager inflating other fragments with no luck.
How can I get it working?
Thank you in advance!
Try to put setHasOptionsMenu(true); inside the fragment where you want onOptionsItemSelected() to be called.
Documentation
The fragment should call setHasOptionsMenu(true), and should implement onCreateOptionsMenu() and onOptionsItemSelected().

How can i set back navigation button in my action bar?

I am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly.
Is there any way to code on a button such that it work same as back navigation button of phone?
Along with
actionBar.setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can try using this in Activity, too:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So your menu item "Home"'s click will be handled using that.
I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
and ofcourse:
actionBar.setDisplayHomeAsUpEnabled(true);
You may/may not used this, its up to you :)

understanding onOptionsItemSelected

I have 2 activities with action bar which has this code in its onCrate method:
getActionBar().setDisplayHomeAsUpEnabled(true);
And here is the onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.icon:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The first activity has a parent activity (configured in the manifest) the second one just opened from fragment (without parent fragment configured in the manifest). In the first activity, when I click the icon, the activity item.getItemId() value is the same as android.R.id.icon which then falls in the switch statement (case: Android.R.id.icon). In the second activity those values are deferent. Why this is happening? I would like to fall in the case: Android.R.id.icon in both activities.
Instead of using android.R.id.icon use android.R.id.home
Try this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
For more detail check this link
Use this type of code
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "Done").setIcon(R.drawable.img_done)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(1, 2, 2, "Save").setIcon(R.drawable.img_save)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
// Write your code for the first button
break;
case 2:
// Write your code for the second button
break;
}
return true;
}
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.icon: //where 'icon' would be your item ID from menu.xml.
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you are using android.R.id.icon that might be wrong. Instead of that you may want to use R.id.icon.
There is a difference between both R.id.icon and android.R.icon files.
Also make sure that you are not using following import
import android.R;
It may lead you to unwanted results.

Categories

Resources