I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack
Related
In my main Activity, I have a fragmentContainerView in which I am changing the fragments by using bottomNavigationbar which has 2 navigation Items (home & profile).
Now I want to show OptionsMenu the "profile" fragment is shown and then again remove it when the "home" fragment is shown.
How can I do it? or is there an alternative?
Create a public method in the activity to change set the option menu and call it from the fragment like below.
HomeActivity homeActivity=(HomeActivity) getActivity();
assert homeActivity!=null;
homeActivity.yourPublicMethod();
I have a 2 fragments, fragment adapter, sliding tab layout, sliding tab strip and main activity.
How do i change the action bar text when i change tab like when user select tab1 the action bar text is home and tab 2 action bat text is profile.
also how do i change to tab text to icon.
for my final year project please help thanks :D
Guide for the sliding tab: http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
it crashed when i add
getActivity().getActionBar().setTitile("required title as per
fragment");
in the fragment #Override onResume
LogCat error Attempt to invoke virtual method 'void
android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null
object reference
In onResume() of each fragment you can get actionbar like getActivity().getActionBar().setTitile("required title as per fragment");
i hope this will solve you problem
I am loading an activity from my fragment. If I press the back button on the device, it goes back to the Gridfragment (as expected).
But, if I click the action bar back arrow, it goes back to the mainActivity hosted fragment = HomeFragment and not the Gridfragment.
From HomeFragment - ( default fragment)
Fragment f = GridFragment.newInstance(GridFragment.TAG_bundlePhotosList);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).addToBackStack(null).commit();
Fragment Grid:
Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
i.putExtra("from_grid", "from_grid");
tartActivity(i);
ImagePreviewActivity :
//Display back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ImagePreviewActivity);
This is the same problem, and as such the same solution as here.
When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.
I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()
I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?