Updating navigation drawer after back press - android

I'm implementing an Android application with a Navigation Drawer and I have a problem with it.
I started from the template (Android app with navigation drawer) in Android studio and added a new section to it - Settings. When clicking the settings button the user is taken to a new Activity and the user can go BACK either by pressing the "up" button in the action bar (Which works perfectly) or by pressing the physical back button. That part doesn't work so good.
When pressing the "up" button and then opening the navigation bar, my home page is highlighted (selected). But when using the back button and opening the navigation drawer, the Settings item is selected, as if the user is viewing that page and not the home page.
So this is because the Navigation Drawer fragment isn't updated and I havent found any way to solve this problem. What I would like is for the Navigation Drawer to be recreated (Which is what I think happens when pressing the "up" button). Do you know how I can make this happen?
I started working on a solution based on this:
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
}
});
But I don't know what to write there that will update the whole UI. (experimented with calling onCreate() but it's too ugly and can't be the right way).
Any suggestions?
UPDATE:
The onBackStackChanged event isn't sent when pressing BACK from another activity. Only for fragments in the current actvity.

You have to override the actionbar back button like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
//or
super.onBackPressed();
break;
}
return true;
}

I had the same problem.I used notifyDataSetChanged() in the ListView Adapter.
In getView() i used,
if(mListView.isItemChecked(position)) {
convertView.setBackgroundColor(getResources().getColor(R.color.md_black_1000_12));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
notifyDataSetChanged();

Related

Perform an action on press of back arrow in navigation ui

I am using the navigation ui with single activity. In a particular fragment, I would like to perform an action when the arrow key on the toolbar is clicked. The toolbar currently works (takes user back to previous fragment), but I would want to perform an action before that is done (the action may even be to not go back to previous fragment).
The solution turned out to be using setNavigationOnClickListener on the toolbar instance, checking if the destination you are in matches the destination you wished to have that custom behavior, if yes, you perform the custom action there, if no, you navigate up normally. Here is the code snippet:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (navController.getCurrentDestination().getId() == R.id.addPersonalNoteFragment) {
System.out.println("do what ever you want to do on the back arrow press here");
} else {
NavigationUI.navigateUp(navController, appBarConfiguration);
}
}
});

Use UP caret in ActionBar to go from Activity to Fragment - Android 4.0+

I've been researching all day/night for a solution, but it seems there are lots of options to go from an Activity to a Fragment, but none are working for me on S.O. In practice, I am in an Activity, and I want to use my app logo in the ActionBar to click it and then return to a Fragment. This Fragment is the "parent class" of my Activity, meaning there was a button in the Fragment I clicked that took me to my Activity.
But I can't get all the code snippets I've seen to work.
I have put this in my onCreate() of my Activity:
// Shows the up carat near app icon in ActionBar
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I also included this in my onOptionsItemSelected() method of my Activity:
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
return true;
default:
return super.onOptionsItemSelected(item);
}
The result is that I see a "back button" carat (as shown below), but when I click it nothing happens. I'm supposed to go back to the Fragment I came from. FYI, my Fragment class actually extends Fragment (not FragmentActivity). My Activity extends ActionBarActivity, so I am looking for an answer that will work for Android 4.0+. Also, my Fragment does not need the same instance (necessarily) when it is returned to. It only has buttons on there, so a new instance is fine, if it gets created, upon returning.
Thanks for your help!!
One small line was needed: finish(). Since the FragmentManager pops off one item in its backstack, by using fm.popBackStack();, it still needs some sort of action to go to the previous fragment. Adding finish() enabled the current Activity to end. The line in context:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The rest of the code I had above was correct and is needed too, to make it all work. Now, I am able to navigate to my NavigationDrawer fragment, click a button there to go to an Activity, then press the navigation UP caret to return to my Fragment at anytime. This was tested successfully on a Samsung Galaxy5 phone.
One thing that you do not read about in the Navigating Up with the App Icon section of the ActionBar Android doc is that since you are using a fragment to return to, you cannot use their <meta-data> tag instruction to specify the parent activity in the manifest file, because you are not returning to an Activity! But rather a Fragment. So a workaround had to be achieved by using FragmentManager.

Android Home Up Button -- Specify the Parent Activity Fragment

I am using a Navigation Drawer on the main activity of my app with three options. Option one is the default fragment being used in the main activity. Clicking option two just replaces the old fragment with a new, but stays in the same main activity. This is all good.
Next step is that I click one of the list items in option two and it opens a new activity+fragment. I implemented the Home Up button in the new fragment with
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The problem comes up with I hit the Home Up button. It takes me to the main activity with the initial fragment (option one) and not the option two fragment. How do I change this?
You need to override onOptionsItemSelected in Activity two and just finish it when home pressed
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Clicking the top home button doesn't do anything

I am using actionbarsherlock. I have three screens as follows
lists items in list activity
lists items in list activity after an item from first screen is clicked
shows details about the clicked item.
On second screen I want to have home button that takes the user back to first scree, and on third screen I want to have a home button that takes the user back to second screen.
Here is what I'm doing:
//on second activity:
getSupportActionBar().setTitle("First");
getSupportActionBar().setHomeButtonEnabled(true);
//on third activity:
getSupportActionBar().setTitle("Second");
getSupportActionBar().setHomeButtonEnabled(true);
This makes the button on top left show up but nothing happens when I click it.
I found online that this change needs to be made in onOptionsItemSelected as well. But I don't have android.id.home in my code. My IDE throws error on android....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == /*what should I put here*/) {
finish();
return true;
}
return true;
}
you should put here android.R.id.home - it is an Android home button id

Show/Hide DrawerLayout depends on current fragment

I have an Activity class that have a DrawerLayout. That layout shows a list in Drawer and let user to switch between fragments. In these Fragments, there are some URLs and when user clicsk on that, a WebviewFragment would be shown. However, I don't want to show the DrawerLayout in the WebViewFragment. Instead, I would prefer user would being redirected to previous Fragment.
Is there any way for me to show/hide the DrawerLayout depends on what the current Fragment is?
I try to call mDrawerLayout.setVisibility(View.GONE), but it seems that it is not complete. At least the ActionBar icon is still the drawer icon.
You can use this method to lock or unlock the drawer: DrawerLayout.setDrawerLockMode(...). (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use DrawerLayout.LOCK_MODE_LOCKED_CLOSED; to unlock, use DrawerLayout.LOCK_MODE_UNLOCKED.
If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// check lock mode before passing to ActionBarDrawerToggle
// I assume your drawer is on the left; if not, use Gravity.RIGHT
int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
Why not open a new Activity that has only a WebView as its content instead? Then the user will have to press the back button to go back to the Activity with the DrawerLayout, and then there will be no problems.
Alternatively, you don't have to have such an activity yourself, you can let Android find a browser for them to open instead using this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("HTTP_URL_GOES_HERE"));
startActivity(intent);

Categories

Resources