I'm having problems with SettingsActivity which was generated by Android Studio. Specifically pressing the Up button creates a new instance of settings activity instead of going back to the parent activity. This happens only on large tablets where settings are split - i.e. headers to the left of the list of settings.
The following code was generated by Android Studio:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
On large tablets onMenuItemSelected() always returns true so navigateUpFromSameTask() is never called. However, even forcing the call to navigateUpFromSameTask() produces the same results.
Example:
...
if (!super.onMenuItemSelected(featureId, item) ||
isXLargeTablet(this)) {
NavUtils.navigateUpFromSameTask(this);
}
...
The only thing that seems to work is a call to finish().
Example:
if (id == android.R.id.home) {
if (isXLargeTablet(this)) {
finish();
return true;
}
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
The parent activity's (main activity) launchMode is set to "singleTop" in the manifest.
The same navigation works fine on smaller tablets and phones.
I'm not sure if my hacky solution is sufficient.
Is there another way to deal with this 'bug'?
Thanks.
Related
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...
I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back.
Thanks.
You can do this.
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home
Call method onBackPressed();
here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
I am working on animating an element between two activities using code like this:
Pair<View, String> pair1 = Pair.create(sharedView, transitionName);
Pair<View, String> pair2 = Pair.create(sharedBackgroundView, transitionBackgroundName);
ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, pair1, pair2);
startActivity(myIntent, transitionActivityOptions.toBundle());
This works great when I`m using phone's back button to get back tot the initial activity, but when I use back button from the Action Bar the transition animation is no longer present. I there any work around for it?
The problem can be seen in the following clip, first time I use phone on screen back button and second time I use Back button form the Action Bar.
https://www.youtube.com/watch?v=Wtdcw2fGBTg
Here is better solution I'm using.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finishAfterTransition();
return true;
}
return false;
}
Note: finishAfterTransition() call requires Minimum API Level 21.
Intercept the click on home button and call the super onBackPressed()
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onClickHome();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onClickHome() {
super.onBackPressed();
}
How I can change working of home button in android? I want to when I click on home button I do some actions and after that application should go to background. How I can do that.
Write your own home screen. When the user presses HOME, they will get a choice of running your app or the built-in home screen. They can elect to choose one just this one time, or set either app as being their default from now on. Many will wonder why on Earth you decided to decided to implement a home screen.
Most developers care about any case where their activity moves to the background, in which case you can either use a lifecycle method (e.g., onPause(), onStop()) or try onUserLeaveHint().
I do it this way:
/* Handles item selections */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//You can do whatever you want here
Intent homeInt = new Intent(this, SomeActivity.class);
startActivity(homeInt);
return true;
}
return false;
}
Have you tried overriding onKeyDown()
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// ENTER CODE HERE
return true;
}
return super.onKeyDown(keyCode, event);
}
I've handled screen rotation config changes in the android manifest, which works for dialog themed activities, however for these menu groups, which open after selecting a menu item (in onOptionsItemSelected) still close when I rotate the screen. Can I handle these in onConfigurationChanged? Or is there a better way? I've attached the code that opens the submenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.submenu) {
if (item.getItemId() == this.submenu) {
return true;
}
this.value = item.getItemId();
item.setChecked(true);
//do something with value
return true;
}
//...
return super.onOptionsItemSelected(item);
}
you will need to override
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
But to do this you must specify the configuration change you will handle yourself by adding to the manifest file on the Activity level the tag
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "orientation", "screenLayout",
"fontScale", "uiMode"]
on onConfigurationChanged save the state and reload it on onResume
This ended up being a very simple fix. I needed to add the line android:configChanges="orientation" to my activity in the AndroidManifest.