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.
Related
My android application has the following activity flow
Listing -> Buy -> Sell
From the listing when an item is clicked/selected an id is passed to buy to pull the relevant data from the server. If a user owns a certain item there will be a sell option as well from the buy screen (which doubles as an overview of said item).
The problem I am facing is that when you are on the sell screen and press the android back button you are taken back to the buy screen in it's original state, however when you click the back arrow (up) from the activity in the toolbar, it seems that it is actually trying to launch a new activity and throws an exception (since the activity cannot know what the id was). The buy activity is listed as a parent to the sell activity in the manifest.
I need to somehow make the up button to act the same as the back button on this particular activity, or at least pass the id back somehow.
Any suggestions would be appreciated.
If you're using an ActionBar (either platform or AppCompat) override the following method in the activity from which you wish to go back.
#Override
public boolean onOptionsItemSelected(#NonNull final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
This should be safe to use in a simple navigation hierarchy such as yours.
Just yesterday i did this for my app. This is what I did:
In manifest file I removed
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
that was part of my DetailsActivity.
In the DetailsActivity I added this line to onCreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and in onOptionsItemSelected() method I added these lines
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
Not sure if it is the best way to handle this but oh well it works :)
In your Activity, onOptionsItemSelected() method get the clicked item id, if the id is android.R.id.home then just finish() current activity.
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm trying to implement a Navigation Drawer, I can open it by sliding my finger, however, when I click on the home button on my action bar nothing happens. I tried logging the item id in the onOptionsItemSelected() method and I confirm the id I retrieve is different from R.id.home. How do I retrieve the home button's id?
The id for home is not R.id.home, it is android.R.id.home because it is a home button created by android itself. using android.R.id.home in onOptionsItemSelected() will hopefully works for you.
Use
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
//do something
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
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);
}
Is it possible to display both home icon and back icon in the toolbar?
1) Is it possible change the order of display of back button icon and home icon.
Currently it displays the arrow button first and then the logo (home button)
2) Second requirement is to clear the activity stack on clicking the home icon and going back to previous screen in case of back button.
I have the following code which will display a arrow back key and home icon which is set as logo. Is it possible to handle click events on both these icons:
Toolbar toolbar = (Toolbar)findByViewID(R.id.toolbar);
toolbar.setNavigationIcon(R.drwable.btn_back);
setSuppportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.home_icon);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I am able to handle to the click on arrow icon by handling it in onOptionsITemSelected method.
Is there a way to handle click on logo icon?
My idea is to use the home button click to clear the stack of activities and use the back button to navigate back to previous screen.
I tried with
toolbar.setNavigationOnClickListener()
but it has no effect on back button click.
Handling android.R.id.home works when handled in
onOptionsItemSelected()
For navigating back. This worked for me.
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
try with this
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getActivity().finish();
}
return true;
}
});
Design our custom layout as a separate "toolbar_content.xml" and include this layout inside toolbar tag in your "main_layout.xml".
Write click listeners for your items in "toolbar_content.xml" in your base activity so that listeners will be available thru out the app.
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);