onBackPressed not called from Toolbar - android

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

Related

Android activity doesn't return from toolbar to main activity

I am having an activity with a toolbar and when I press to go back to main activity the whole application is closing.
I put the parent page in the AndroidManifest.xml :
<activity
android:name=".FindFriendsActivity"
android:parentActivityName=".MainActivity" />
And in the java activity file in onCreate method :
FindFriendsToolbar = (Toolbar) findViewById(R.id.find_friends_toolbar);
FindFriendsToolbar.setBackgroundColor(Constant.color);
setSupportActionBar(FindFriendsToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setTitle("Find Friends");
I don't understand where I am wrong because I did the same thing in some others activities and it works.
Inside onCreate method write this
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Outside onCreate method write this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
make sure your theme has actionbar implemented

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

How to get Up Navigation behavior exactly like back button pressed

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.

How to back to previous Tab layout specific tab(Fragment) from another activity?

First of all, I have search through on StackOverflow and read some of the related posts but it doesn't really fix my problem. From the screenshot, you can see that I have added some coding to the AndroidManifest which will add an "Arrow" on the top left-hand corner - it serves as a back button.
AndroidManifest.xml
<activity android:name=".MainStory"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
On my MainActivity Tab 3 Fragment
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getActivity(),MainStory.class);
startActivity(intent);
}
So from the screenshot, I am at Tab 3 with a ListFragment. After clicking either one of the item, it will go to MainStory activity. If i click the device "Back button" , it will go back to MainActivity Tab 3(this is what I want).
However, When i click the "Arrow" it will return to MainActivity Tab 1 but not Tab 3 of MainActivity.
So my question is how to go back to Tab 3 Fragment just like the device "back button" when clicked on it?
Try to do something like this on the toolbar:
protected Toolbar setUpToolBar(String title) {
Toolbar toolbar = (Toolbar) findViewById(R.id.appbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(Html.fromHtml("<font color=\"#ffffff\">" + title + "</font>"));
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
} else {
AMLog.d("Action bar is null");
}
if (toolbar != null) {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
return toolbar;
}
Do this, it worked for me:
#Override
public boolean onSupportNavigateUp() {
finish(); // Returns you to the last tab that you were before navigating
return false; // Cancels out the default action that takes you back to the 1st tab
}
Hope it helps,
Cheers

why Actionbar title keep displaying the backstack title on hardware back button pressed?

I'm trying to change the title of activity, where i'm going to the activity from the fragments, so when i click on the hardware back button the title doesn't chnage to the one that i have provided in that activity i.e
this.setTitle("Something");
I have also tried
setTitle("Something");
Update
When i use the below mentioned code i run into a problem where the title that i assign in the mainActivity stay in all the pages, hence in the frgament getActivity().setTitle(""); seems to be useless in this case, so instead of changing everywhere the title using the below mentioned code i.e changing the actionbar title using getsupportactionbar.settitle(""), is there an easy way to do.
By any means can i know if an activity has came from the system back button back state or not?
You can handle this situation with onResume() method
//...
#Override
onResume(){
super.onResume();
this.getActionBar().setTitle("Something");
}
ı think, that will help you.
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
EDIT:
For SupportActionBar use:
ActionBar actionBar = getSupportActionBar();
EDIT2:
I use this to set Title from every fragment
#Override
public void onResume() {
super.onResume();
// Set the title
getActivity().getActionBar() //getSupportActionBar()
.setTitle(R.string.fragment_title);
}
If you always add your fragment to backstack when changing, then backpress can be override as follows to always get your previous fragment's title:
#Override
public void onBackPressed() {
int T= getFragmentManager().getBackStackEntryCount();
if (getFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
else if (getFragmentManager().getBackStackEntryCount() == 1) {
finish();
}
else {
String tr = getFragmentManager().getBackStackEntryAt(T-2).getName();
setTitle(tr);
getFragmentManager().popBackStack();
}
}
If you are occured this in Kotlin just remove Label from destination in Your navigation graph.
getSupportActionbar().setTitle("Your Title")
You have to call this whenever you want to change your title.
If you are switching between fragments call the method again and switch it back to your old title.

Categories

Resources