How to detect if the Up button was pressed - android

In my activity the action bar shows only the left arrow and the title of the activity.
When I press the left arrow the activity goes back to the previous activity, but no event is registered in the onKeyUp, OnkeyDown and OnBackPressed methods.
But when I press the Back key on the phone (at the bottom) the activity goes back to the previous one and all the methods onKeyUp, OnKeyDown and OnBackPressed register an event (in the logcat).
How can I capture that left arrow (I think it is called the UP button)?
The reason I need to capture the key is to know in the onPause method that the activity is destroyed by the user and not by the system (for example, if the user switches to another activity).
By further investigating he matter I found out that the UP button gives an event that is captured by the onOptionsItemSelected method and since there is no other button on the menu I know it is this button.

see http://developer.android.com/guide/topics/ui/actionbar.html#Handling
Handling clicks on action items
When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using the MenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the tag's id attribute so you can perform the appropriate action. For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
onUpButtonPressed();
return true;
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Note: If you inflate menu items from a fragment, via the Fragment
class's onCreateOptionsMenu() callback, the system calls
onOptionsItemSelected() for that fragment when the user selects one of
those items. However, the activity gets a chance to handle the event
first, so the system first calls onOptionsItemSelected() on the
activity, before calling the same callback for the fragment. To ensure
that any fragments in the activity also have a chance to handle the
callback, always pass the call to the superclass as the default
behavior instead of returning false when you do not handle the item.
To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}

Yes you are right, you can detect if the up button was pressed in the onOptionsItemSelected method. This should work:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do something here. This is the event fired when up button is pressed.
return true;
}
return super.onOptionsItemSelected(item);
}

Related

is there a way to make actionBar back button resend an intent with putExtra?

I have an activity that has a recyclerview that populates its data based on the yearSelected string forwarded from the previous activity with an intent and put extra. then I choose a row and navigate to the next activity to show the week selected info. when I want to return to the previous activity using the action bars back button it crashes, I'm guessing because I'm no longer sending the intent info when I return back. is there a place I can use putExtra when it returns back or is there a way to set the back button to just return to the previous activity like the back button on the bottom does?
Actually, the action bar should behave like a menu.
You can listen for the "back" button and perform whatever action you need to in onOptionsItemSelected..
eg
public boolean onOptionsItemSelected(MenuItem item) {
...
switch (item.getItemId()) {
case android.R.id.home:
<set return result etc and finish>
return true;
...
Note that the back button item id is android.R.id.home .. because the same item is used for home as up (and hamburger menu from memory).
Also, finishing and activity in the middle of handling an event isn't great practice, but commonly done.
override fun onOptionsItemSelected(item: MenuItem): Boolean{
return when (item.itemId){
android.R.id.home -> {
finish()
true
}else -> super.onOptionsItemSelected(item)
}
}

Android activity to a fragment control transfer

I am creating an android application with three tabs using PageSlidingtabStrip as a library to create a swipe view.And it has three fragments.Each fragments has a list view.When the item of the listview is clicked it opens an activity and display the details.
The problem is how can i come back to the fragment in the main screen using back button in actionbar in the activity
And how can i go to the corresponding Fragment(Tab)
Try something like this :
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent intent = new Intent(YourCurrentClass.this , ClassThatYouWantToGo.class);
startActivity(intent)
}
Or actually like #TommyTopas said, you can just Override onBackPressed and put this.finish();.
EDIT
As I've understood you want to use a button on your AcitonBar, then you have tod o something like this :
First set the HomeButton enabled doing :
getActionBar().setDisplayHomeAsUpEnabled(true); Then Override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// or onBackPressed();
this.finish()
}
return true;
}
As I understand, when you return to the "Tab" Activity, you want to display the same tab in which the list item had been clicked. What you can do is, when a list item in any tab is clicked, save the tab number in onSavedInstanceState(), and when the Activity is recreated, then set the previously selected tab (if one was selected previously). You will get the savedInstanceState that you saved in onSavedInstanceState() back in the onCreate() of the same Activity.
You can provide an Up navigation by writing getActionBar().setDisplayHomeAsUpEnabled(true); and then in the onOptionsItemSelected method in the activity, if the item's id is android.R.id.home call the activity's method onBackPressed(); which will close your current activity and come back to your fragment

Lollipop activity transitions: Back button vs. back from toolbar differences?

I have an app where I'm doing activity transitions for a company directory. When a search result is selected, the animation of their photo to the detail screen works. And if I hit the back button, the reverse animation occurs. However, if I hit the back arrow from the Toolbar, the reverse animation never occurs.
The detail screen is a new DetailActivity with a single fragment in it called DetailFragment. I'm not doing anything special on "onBackPressed". Should I be?
If you want the return transition to play, you'll need to listen for the up-navigation button click and call finishAfterTransition() directly:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}

Action bar item click handler in fragment

I have a simple 2 activity application. The main activity populates a listFragment, and the second activity populates a fragment with fields to add a custom object (list items) to the main activity.
In the second activity I have a "save" icon in the action bar. I'm trying to figure out how to listen for this button click in the fragment, so I can package up the textFields and pass it back to the activity via the interface.
I tried to override onOptionItemSelectedbut it doesn't ever hit. How would I handle this?
Okay, so the trick is in the fragments onCreate method, you have to call
setHasOptionsMenu(true);
then all you have to do is override the onOptionsItemSelected in the fragment, and handle the action bar click there!!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save : {
Log.i(TAG, "Save from fragment");
return true;
}
}
return super.onOptionsItemSelected(item);
}

Action bar back button from un activity to previous activity fragment

I'm using a navigation drawer in a main activity with many fragments
For example :
I have fragment 1 for presentation
And fragment2 that contains a listView.
If I click an item in the listView it will launch another activity
When i click the action bar back button i want to go to the main activity fragment2
But the fragment1 is used
How can I use the second fragment
Thanks for your answers
In your second activity's onCreate, put(although i think you already have) :
getActionBar().setDisplayHomeAsUpEnabled(true);
and inside second activity's options handling(i think you've done this too) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and inside your first activity's onCreate's intent handler for example :
if(Intent.ACTION_VIEW.equals(action))
you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.
I have added the code below in my second activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return true;
}
It's working as I expected
is it a clean solution ?
Thanks

Categories

Resources