Android Home Up Button -- Specify the Parent Activity Fragment - android

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

Related

Updating navigation drawer after back press

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();

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.

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

Need suggestions with ActionBar, Navigation Drawer and back stack

I have an ActionBar activity. In this activity I have implemented Navigation Drawer from Android API.
One option of navigation drawer set a ListFragment with some elements inside its list.
When I click some elements I want to create a new fragment and set previous ListFragment to the stack. Also I want to destroy this new fragment by clicking ActionBar home button, in order to return to the previous ListFragment.
My problem comes here: When I click home button of the actionbar, drawer layout is displayed, instead of destroy the fragment... What should I do?
I have Overriden onOptionsItemSelected method in the fragment:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
getFragmentManager()
.popBackStack();
}
return (super.onOptionsItemSelected(menuItem));
}
Also I have set ListFragment to the backstack when Inflating the new fragment:
getFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.replace(R.id.activity_main_fragment_container, fragment)
.addToBackStack(null)
.commit();
Well, as the docs say here, popBackStack() is async. I suppose that the drawer layout is displayed because you call super.onOptionsItemSelected(menuItem).
I suggest you to return true for all cases you handle this selection by yourself (in this specific situation: case android.R.id.home:), and call getActivity().onBackPressed() (assuming that by pressing hardware back button the last fragment gets removed, as it should) instead of popping back stack directly. I've implemented a similar solution and it works for me.

Using ActionBar Up Navigation in master/detail fragments

I have an app with master/detail layout (1 activity, 1 ListView fragment and 1 detail fragment). When the user clicks an item in the ListView, a fragment transaction instantiates a detail fragment on the right-pane that includes the information corresponding to that item. When the detail fragment is shown I hide the initial action bar buttons/items and show 3 new AB items (done/delete/cancel). The user can clean the right-pane and return to the initial UI state by either pressing the back button or by pressing one of the 3 AB items.
The issue I'm experiencing is that when the user selects the app's home icon (i.e. "up navigation") the activity gets re-loaded (i.e. the animation that indicates that the activity is starting can be seen as both the action bar and the UI is been redrawn). The issue only happens when the app home icon is pressed. If the user presses the back button or a cancel/done/delete action bar button, the fragment is simply remove from the right-pane and the UI returns to initial state without any "re-loading".
The XML layout for the activity is the following (inside LinearLayout; prettify is hiding that line):
<fragment class="*.*.*.ListFragment"
android:id="#+id/titles" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout android:id="#+id/details" android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" />
The DetailsFragement has the actionBar.setDisplayHomeAsUpEnabled statement in its onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ActionBar actionBar = getSherlockActivity().getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
For both the ListView fragment and the Detail fragments the onCreateOptionsMenu() and onOptionsItemSelected() method are implemented within the fragments. Below the code for the Details fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.edit_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// some variable statements...
switch (item.getItemId()) {
case android.R.id.home:
//Toast.makeText(getSherlockActivity(), "Tapped home", Toast.LENGTH_SHORT).show();
onHomeSelectedListener.onHomeSelected();
return true;
case R.id.menu_edit_item_done:
editedTask.setTitle(editedTaskTitle);
onTaskEditedListener.onTaskEdited(editedTask, UPDATE_TASK, true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In the host activity I implement the onHomeSelectedListner to handle the app home icon press (i.e. "up navigation":
public void onHomeSelected(){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
TaskFragment taskFragment = (TaskFragment)getSupportFragmentManager().findFragmentById(R.id.details);
ft.remove(taskFragment);
ft.commit();
manager.popBackStack();
}
The activity's listener in charged of handling all other action bar buttons (i.e. done/delete/cancel) is onTaskEditedListener and, aside of other code that processes some data, it has the same fragment transactions shown above.
Update(1/24)
Based on tyczj and straya feedback I placed log statements inside onCreate(), onResume(), onPause() of the activity to determine the differences between onHomeSelected and onTaskEdited listeners. I'm able to confirm that during the "up navigation" event (i.e. onHomeSelected) onPause(), onCreate() and onResume() are called. Whereas during the onTaskEdited call (i.e. back button or done/delete/cancel press) none of those events are called.
Update (1/25)
Based on a suggestion by Mark Murphy, I commented out the onHomeSelected method call in the "case android.R.id.home" statement just to see what would the Activity do. The thinking was that the app would do nothing since the are no statements. Turns out that is not the case. Even without a call to the listener method (i.e. that removes the fragment), the activity is restarted and the detail fragment is removed from the fragment container.
Update (2/28)
I temporarily workaround the fact that my main activity was getting restarted by disabling the window animations (as highlighted in my own answer). However, through further testing I uncovered a bug. Thanks to Wolfram Rittmeyer's sample code I was able to figure out the real reason(s) why my activity was restarting (in master/detail single layout) during up navigation:
1) Although I was using this "onHomeSelectedListener" to properly remove the fragment from the backstack, I still had some remnant code in the ListView fragment's onOptionsItemSelected that was creating a new intent to start the hosting activity. That's why pressing the app's home icon was re-starting the activity.
2) In my final implementation (shown in my own answer), I got rid of the onHomeSelectedListener in the activity and replace the startActivity intent (i.e. offending code) inside the ListView's onOptionsItemSelected to use the fragment removal + popBackStack code originally in the onHomeSelectedListener.
After much research and poking around, turns out that only reason why my activity was restarting during "up navigation" for master/detail configuration was because I left some code in the ListView Fragment's onOptionsItemSelected that was creating an intent to start the main activity in addition to my full fragment transaction code elsewhere. Below is the final implementation with which I got "up navigation" to work properly on both phone (multiple activities) and tablet (single activity/multi-pane) configurations. Thanks to Wolfram Rittmeyer for a couple of hints in his code (link in the comment section) that help me pinpoint my problem!
Main Activity: Hosts the fragments and performs some other app-specific operations
ListView Fragment: Handles "up navigation in table configuration
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDualPane){
FragmentManager manager = getSherlockActivity().getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
DetailFragment detailFragment = (DetailFragment)manager.findFragmentById(R.id.details);
ft.remove(detailFragment);
ft.commit();
manager.popBackStack();
getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(false);
}
return true;
// Other case statements...
default:
return super.onOptionsItemSelected(item);
}
}
Details Fragment: Handles up navigation in phone configuration
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// Sets "up navigation" for both phone/tablet configurations
ActionBar actionBar = getSherlockActivity().getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(!mDualPane){
Intent parentActivityIntent = new Intent(getSherlockActivity(), MainActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
getSherlockActivity().finish();
}
return true;
// Other case statements...
default:
return super.onOptionsItemSelected(item);
}
}
If you look at the Navigation Design Pattern you will see that you want to return to the starting activity when the home button is hit.
So say you have 2 Activities call them A1 and A2. Clicking on something in A1 takes you to A2. If the user hits the home button you should return them to A1 clearing the stack of everything up until that activity like this
Intent intent = new Intent(this, A1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this is what the flag Intent.FLAG_ACTIVITY_CLEAR_TOP does
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.`
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
don't: break and then return super.onOptionsItemSelected(item), rather just: return true;
UPDATE:
So you're saying the Activity is "restarted" based on what you see happen with Views, but can you confirm what may or may not happen to the Activity (and Fragments for that matter) by using logging in the various lifecycle methods? That way you can be sure of what the current (erroneous) behaviour is before moving forward with diagnosis.
UPDATE:
OK, good to be sure about behaviour :)
Now regarding your question "What is the correct way to implement "up navigation" for a master/detail layout (1 activity/2fragments)? ":
The typical way is that the 2 Fragments got added within a single FragmentTransaction and you simply popBackStack to remove them and go back to whatever previous state was. I think you're doubling up by manually removing a Fragment within a FragmentTransaction and then popping backstack. Try just popBackStack. Oh and just to be sure and consistent, since you're using ActionBarSherlock and support.v4 are you using a FragmentActivity (rather than an Activity) and SherlockFragment?
I think you should handle the Up button only inside the activity.
If youre in a phone, the up button will be handled by activity that acts as a wrapper of that fragment, in tablet (master/detail pattern) you dont want it anyways

Categories

Resources