Hi I need to handle back button on fragment. I using this navigation on my mobile apps. The question is how to handle back button when I open new fragment page?
I try using below code on new fragment.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
But when click the back button , the navigation will be open. Any solution ?
Thanks
As you know whenever user presses Back button you need to go to the previously loaded fragment and to do that try this (note fragment must be added with transaction.addToBackStack(null);)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
int backStackCount = fragmentManager.getBackStackEntryCount();//check currently how many frags loaded
if (backStackCount > 0) {
fragmentManager.popBackStack(); //go back to previously loaded fragment
}
}
return super.onOptionsItemSelected(item);
}
Related
Iet's say I have 3 tabs, i use slidingTabLayout
so 3 tabs are 3 fragments.
i use toolbar, so each fragment has toolbar with textview and icons.
for fragment 1 and 2, icon is the same "search icon", if click it, will go to another activity for searching something.
if user at "search activity" there is a "back arrow" on tool bar, click it, user should go back to previous fragment.
if user in fragment 1, and then he click "search" he go to "search activity", and after searching, he click "back arrow", he should go back to fragment 1.
but if user in fragment 2, he click "search" he go to the same "search activity", and after searching, he click "back arrow", he should go back to fragment 2.
so i googled "click go back arrow , from same activity back to different fragment"
i did not find clue ...i think it might be something with "fragment Manager", "backPressed"....i lost myself now
I guess i might not use the correct words for google.....
I think this could work.
Have a searchable activity, a link for that is here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
In the Search activity, have something like this in the OnCreate method:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
And then also in the search activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
Hope it works!
I think your problem can be solved in onBackPressed() function.
It works like a back button.
You can try to add your arrow icon like this.
public void onClick() {
onBackPressed();
}
Hope it helps.
I have only one activity in my application which contains navigation view to navigate through 3 fragments.
When I navigate to a fragment I get the hamburger menu icon changed to the up button in the onNavigationItemSelected method like this:
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The up button appears well but has no effect yet.
I wanna navigate to the previous fragment in the stack when the button is clicked but I can't get to add a listener to that button.
I've tried using:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
but this is never called. I think it's not called because I have no parent activity in the manifest.
Thanks in advance.
You should not call onBackPressed, you should use fragment transaction method to to transact between fragments like I replaced my current fragment with previous fragment using replace method. container is the layout containing the fragments.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commit();
I hope, it give you some direction.
if (id == android.R.id.home) {
//put the code like above here.
return true;
}
I am a newbie in android and I have problem when I made my application with material design. I follow this tutorial and create a fragment that allow searching tickets as this picuture .
After search button is clicked, fragment Result will be display such as this .
But I don't know how I can set action for back icon in toolbar to back previous fragment. When I draw back icon, it always show navigation drawer and i just can back previous fragment when I touch back icon on my devices, but when i do that, the title of toolbar not changing. For example, when I am in fragment resutl, if i click back on devices i will back to Searching fragment, but the tittle of toolbar is title of Result. please help me.
p/s: So sorry if this question is stupid, but I stuck with it more three day. I also search in Google and stackoverflow, but maybe I'm not fully understand about navigation drawer so I try but can not resolved. I'm also sorry if i wrong grammar because my english is not good and thanks you for reading my question
Here is my code to replace fragement searching ticket into Result.
public void DataBundle(ArrayList<TicketInforModel> ticketInforModels){
ResultFragment rFrag = new ResultFragment();
Bundle packageDataStation = new Bundle();
packageDataStation.putSerializable("arrTicket",ticketInforModels);
;
rFrag.setArguments(packageDataStation);
//noinspection ResourceType
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, rFrag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// set the toolbar title
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);//get Toolbar from SearchingTicketActivity
((SearchingTicketActivity)getActivity()).setSupportActionBar(toolbar);
((SearchingTicketActivity)getActivity()).getSupportActionBar().setTitle(R.string.title_result);
toolbar.setNavigationIcon(R.drawable.ic_back);
// toolbar.dismissPopupMenus();
}
Listen for click events on android.R.id.home like usual:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Do your stuff
break;
}
return super.onOptionsItemSelected(item);
}
Note : In such circumstances it's good practice to show the result in new Activity when you pressing the search button and you can get back after finish() the result activity by pressing home/up button in the toolbar.Don't make it so complicated.
Try this
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getSupportFragmentManager.popBackStackImmediate();
}
});
I have a fragment on my main activity which gets loaded on click of item from nav drawer and on click of a list item from the fragment(main screen) I navigate to another activity. If I press the device back button I get my fragment screen(as expected) but if I use a tool bar and enable the the home button by saying
getSupportActionBar().setHomeButtonEnabled(true);
I get my main activity content which was prior to selecting an item from drawer. I have set parent of next activity as the main activity but I need the fragment loaded when i go back.
For my problem I found a solution. on click of the home button in the toolbar I killed my activity using finish() method and it worked just as i wanted. Hope it helps the needy.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if(id == android.R.id.home)
{
finish(); //kill subActivity so as to get same screen which was before going to subActivity
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
You need to add this getSupportActionBar().setDisplayHomeAsUpEnabled(true);
along with getSupportActionBar().setHomeButtonEnabled(true);
I've had a look at this question-
Call OnResume when Back Button Pressed when using Fragments
I've done the same things, as mentioned in the answers, but neither is onResume called, nor onCreateView. It's a transaction from an activity to a fragment, all of which are a part of a single tab. How can I call a method from the first fragment when back button is pressed on the second fragment?
Code-
Bundle data = new Bundle();
data.putString("q", code);
data.putString("type", type);
data.putString("name", name);
viewPager.setCurrentItem(1);
android.support.v4.app.FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
eq equity = new eq();
equity.setArguments(data);
transaction.replace(R.id.root_frame, equity).addToBackStack(null).commit();
The method called when you press the back button is onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (yourCondition = true) {
//do something here
} else {
super.onBackPressed();
}
}
But onOptionsItemSelected is called on the parent activity.
I don't know if the back button is required, but I think it's better to implement a callback from one fragment to the other with an Interface and using a custom button not the back button.
fragment life cycle is directly effected by the host's activity life cycle so you gotta take that in consideration anyway did you try to call the method in onPuase() or in onStart() in the fragment that you are heading for after pressing back ? if that didn't work try to reach the emulator back button from if (keyCode == KeyEvent.KEYCODE_BACK) or you can finish() the previous fragment or activity so the back button will get the user no where and create your own back button to redirect the user and replace the method in it using onClick() .
hope that will work for you good luck