How to get Up Navigation behavior exactly like back button pressed - android

How can I get the Up Navigation button to behave like the back button? When I press the back button all my previous activities remain as the were prior.
When I press the up button my activities return to initial state. I tried hooking up onBackPressed(); to case R.id.home on my onOptionsItemSelected but it still behaves differently.
Visual:
Back Button Behavior-
https://www.flickr.com/photos/151974643#N07/35702817566/in/dateposted-public/ Up Button Behavior - https://www.flickr.com/photos/151974643#N07/35702812926/in/dateposted-public/
Any help would be appreciated. Thank you!

You can do it several ways, put this code in onCreate() method of your activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YOUR_ACTIVITY.this.onBackPressed();
}
});
Hope it helps.

Related

Back Button in ActionBar of App

Generally switching from one activity to another activity hamburger icon is replaced with back arrow. I want to control the functionality of that arrow. I have seen many contents here but most of them were related to hardware's back button. How can I control that?
I am trying the functionality in case of fragments. Also I have Navigation drawer attached with the hamburger icon.
I tried this-
if(id == android.R.id.home){
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, new AmbulanceMap()).commit();
getSupportActionBar().setTitle("Book A Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
but doesnt work as I hoped.
I want my back button to change the fragmentto previous fragment.
I had the same problem once. Just like you, things like checking if Android.R.id.home is clicked didn't work.
But I solved it using that:
Set navigation listener to toolbar:
toolbar.setToolbarNavigationClickListener(v -> onBackPressed());
If it should be within fragment:
Create an public method in activity.
In fragment's onAttach (or later) cast getActivity() to your activity and call method you was defined previously.
Example:
// YourActivity
public void setHomeListener(OnLickListener listener){
toolbar.setToolbarNavigationClickListener(listener);
}
//Fragment's onCreate
((YourActivity)getActivity()).setHomeListener(v -> onBackPressed());
//Fragment's onDestroy
((YourActivity)getActivity()).setHomeListener(null);
And, of course, set home us up enabled to show back arrow.
EDIT
if you don't use labmdas u should use:
(YourActivity)getActivity()).setHomeListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourFragment.this.onBackPressed();
}
});
The back button of the ActionBar is a menuItem so you need to override onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//Your back button logic here
break;
}
return true;
}
Also donĀ“t forget to add getSupportActionBar().setDisplayHomeAsUpEnabled(true); after setting the Toolbar

Back Arrow in Activity

I have 3 tabs with lists of different video.In first tab when I click the item of my lyst the new activity started and play the video,this activity have parent my main activity with lists and this mainactivity have 1tab as main tab.But when I click in item in 2 tab,thid videoplayer started too,but back arrow go to main activity and main activity have 1 tab main,not second.It's very hard,but Im trying to explain:-)
How can I change this behavior,to go to 2 tab when click back arrow?
If when you have a toolbar then just add the following code in the onCreate. What this will do, it will work as the Back key of the smartphone. so it will close the current activity, and bring you back to what ever was on.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { finish(); } });
}

onBackPressed not called from Toolbar

I know a lot of questions are posted regarding this topic, but I can't find a workable solution.
I want the onBackPressed() to be called when I press the left arrow in the tootbar.
I'm using appcompat-v7:23:1:0.
The left arrow is working everywhere in my app but here. I suspect it is linked to the fact I start my activity from a fragment.
Activity_A > starts Fragment_A (extends SupportMapFragment) > which starts Activity_B.
I want to go back on Fragment_A when the back arrow is pressed from Activity_B.
For the moment, the arrow doesn't respond to the click, onBackPressed is not called form the toolbar but it is when the hardware back button is pressed.
I've tried adding
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and I've added this in the manifest:
<activity
android:name=".activity.Activity_B"
android:windowSoftInputMode="stateHidden"
android:parentActivityName=".activity.Activity_A" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.Activity_A"
/>
</activity>
(Activities names are replaced for clarification)
It appears the problem was related to the xml layout of my activity_B
I have a scrollView below the toolbar. That scrollView had a android:layout_below="#id/contact_center" whose id doesn't exists in that activity.
I know it sounds strange but having gone back and forward with the code, I'm 100% sure that this was causing the onBackPress() to not respond.
So for people having similar problems, pay attention to your xml layout, there might be an error in there causing this strange issue. Also, be very varefull with copy-pasting code from other activities ;)
Thanks for the help.
add this in your onCreateOptionMenu
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
also add this
getSupportActionBar().setDisplayShowHomeEnabled(true);
Try this
public void setUpToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar Title");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "here", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}

Android Toolbar with both home and back button

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.

Creating custom back button in android

I have an application that has a menu and depending on what button you press in the menu a new activity is opened. I want to have a back button on every screen that will bring you to the previous screen so I'm wondering how do I go about this?
Here is some code I have used that works:
backButton = (ImageButton) findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
However its not good programming practice for me to put this code in all my activities. How do I go about creating some kind of stack that saves all the pages viewed and using that to return to the previous page?
I have to put a back button in my application so I cannot use the existing one in the ActionBar.
just an Idea
Create a baseClass which extends Activity. In there declare
#Override
public void onClick(View v) {
super.onBackPressed(); // or super.finish();
}
In all activity extend this Base Class. And in every layout in the button put
android:onClick="onClick"
And to make the xml design of button reusable create it in a separate xml. and add it using <include/>
Have you tried using action bar in your activity ? use in every activity
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setTitle(getResources().getString(R.string.app_name));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.app_icon);
}
and handle in
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I would suggest against having a custom back button. Android has a hardware back button. Pressing the haradware back button will navigate to the previous.
I don't think you need a custom back button. I don't think its a good programming practice to override the default behaviour.
You create a backbutton in your activity and implementing the functionality as you have done above. Still the user can use the hardware back button for the same functionality. So you would be providing the same functionality which is redundant.
There is a hardware back button on all android devices, and it does exactly what your lines of codes do, unless overridden to do something else.
You can refer to this answer as well.
I had a similar issue where I was using a back button "Cancel" and a home screen "homeActivity". I ended up solving it using this:
CancelButton = (Button) findViewById(R.id.cancel_button);
CancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent homeActivity = new Intent(getApplicationContext(), DeviceListActivity.class);
startActivity(homeActivity);
finish();
}
});
In kotlin
backButton!!.setOnClickListener(View.OnClickListener {
onBackPressed()
})

Categories

Resources