ActionBar refresh when back button pressed - android

I have a button on an action bar that reflects how many items are in the user's favourites. How can I get this to refresh when the back button is pressed?

try this.
#Override
public void onResume() {
invalidateOptionsMenu();
}

Use this:
#Override
public void onBackPressed() {
//Refresh action bar
}

Related

Toolbar menu item programmatic click

I have this code
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openSearch();
return true;
}
});
into onCreate( ) of my Activity.
OpenSearch function call opens up google now like search view. This only happens when the user clicks on the search action item in the toolbar. In my case I want the search view to open up automatically when the activity starts. How can this menu item click be done programmatically.
I can't call openSearch directly because it needs the menu to be created.
Is there there any callbacks informing that action menus have been created ?
You can try something like this :
tollbarLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tollbarLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tollbar.performclick();
}
}
});

click home button in fragment page to disappear action bar activity title

I am facing weird issue.I am using PageActivity inside three
fragments swipes one after another and so on.
My problem is,when I am in FragmentA page,on click the home
button ,again come back to the application, my action bar title name is
Chapter,which I was added the action bar title name in PageActivity and
after 2 seconds the fragment title was loading with the help of
query.
My only problem is,on home button click in fragment page instead of
activity title name,Fragment page title name have to be shown
directly after enter the application.
Below I am posted the code related to this:
PageActivity.java:
public class PageActivity extends FragmentActivity {
static TextView mTitleTextView;
.......
.......
#Override
protected void onResume() {
super.onResume();
ActionBar(Constants.Titlebarcolor);
........
........
}
public void ActionBar(String color) {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mTitleTextView = (TextView) mCustomView.findViewById(R.id.textviewHeading);
mTitleTextView.setText("Chapter");
}
FragmentPageA.java:
#Override
public void onResume() {
super.onResume();
....
....
}
#Override
public void onPause() {
super.onPause();
....
....
}
#Override
public void setMenuVisibility(final boolean visible) {
//get action bar title for every fragment page
PageActivity.mTitleTextView.setText(DatabaseQueryHelper.getInstance().getPagename(getArguments().getInt(Constants.PAGEID)));
......
......
}
Anyone can give me any suggestion regarding to this.Thank you.
I just moved this ActionBar(Constants.Titlebarcolor); line inside onResume() method to onCrate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar(Constants.Titlebarcolor);
}
So that When I entering into the activity,the action bar activity title will not resume.

Prevent NavigationDrawer from opening on Home Button Click

I have a NavigationDrawer for my main activity, in one instance I show a fragment and the "Hamburger menu" changes into an arrow.
I disabled the ability to open the drawer via swipe when that fragment is shown by using this
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
and it works fine but now the problem is the user can still open the drawer by clicking the home button "Arrow". I want the arrow to act as the back button and not open the drawer so how can I stop the drawer from opening?
I guess you need to use setNavigationOnClickListener() on toolbar to override drawer behavior.
Like -
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// logic to decide if drawer open/close, or pop fragment etc
}
});
This does prevents drawer from opening by clicking on icon. But it does open by swiping. But that you already taken care of. So this should work.
Note This works only after you set ActionBarDrawerToggle by calling
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer){ ...
Put this on your Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getId() == android.R.id.home){
doSomething(); //can be a finish()
}
}
Or, from this answer
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popStackIfNeeded();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mActionBar.setDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
});
You could try overriding onOptionsItemSelected check for the id android.R.id.home and call finish() to go back to your previous activity.
try to override on options item select.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home :
// do what you want
return true;
}
return true;
}

In Android, override a method using Button onClick

I've got a Main Activity which is a search interface, when the user clicks a result in the list, the result details are displayed in a Fragment. The Main Activity has an Action Bar, but in the details Fragment, the Action Bar is hidden. To hide the Action Bar in the Fragment, I'm calling a method in the Main Activity from the Fragment Activity like this: ((SearchInterface)getActivity()).hideABar();. Then when the user clicks the back button, they go back to the search interface and the Action Bar reappears. Doing that is a bit more complex than simply calling a method. I set up an Interface in the Main Activity and override the method in the Fragment like this:
Main Activity:
public interface Callback {
public void onBackPressedCallback();
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(fragBackPressed != null)
fragBackPressed.onBackPressedCallback();
checkActionBarState();
}
...
public void checkActionBarState(){
bar_AB = getSupportActionBar();
boolean barVisible = bar_AB.isShowing();
if(barVisible){
Toast.makeText(getApplicationContext(),"ActionBar WAS Visible",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"ActionBar WAS NOT Visible",Toast.LENGTH_LONG).show();
bar_AB.show();
}
}
Fragment:
#Override
public void onBackPressedCallback() {
Log.d("SSIVideoFrag", "checking action bar state");
}
So now, when the user presses the Back button while in the Details Fragment, they go back to Main Activity and the Action Bar reappears. Now I'd like to add a button in the Details Fragment that does what clicking the Back button does, but I can't figure out how to do it. I don't know how to override the onBackPressedCallback() method from within the onClick of a Button:
back_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//--- what to do here?
}
});
simple:
getFragmentManager().popBackStack();
https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()
ps.: that's not an actual back press, your onBackPressed will not be called. But the fragment will be removed, just like in a back press.

How to implement SlidingMenu.OnOpenedListener correctly?

I want to hide back button when sliding menu is opened, otherwise to show it
How should I implement SlidingMenu OnOpenedListener?
I use this SlidingMenu with ActionBarSharlock.
Here is the solution, for those who are interested in.
Set OnOpenedListener and OnClosedListener to your SlidingMenu object
SlidingMenu menu = new SlidingMenu(this);
To show back button (sliding menu is closed)
menu.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
});
To hide back button (sliding menu is opened)
menu.setOnClosedListener(new OnOpenedListener() {
#Override
public void onClosed() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
});

Categories

Resources