I am adding a Up button to my Android application. The app's minimum SDK is 14, and I am testing it with an HTC phone on SDK version 15.
The activity is a subclass of android.app.Activity (not ActionBarActivity from the support package).
ActionBar style display options includes the homeAsUp flag, and I am able to see the standard arrow. However, clicking on the logo does nothing.
I have connected the debugger, and I am able to see that the onOptionsItemSelected method is not called at all. This cannot be because of misspelt name, because other menu items (e.g., Settings) do work (and I can see in the debugger that onOptionsItemSelected method gets called).
The parentActivityName and meta-data PARENT_ACTIVITY are set correctly (although I believe this would only matter if the method got called).
Is there anything I am missing? And how do I get the up button to work?
I encountered the same issue "onOptionsItemSelected" not called when using the ActionBarDrawerToggle it seems like the solution is setting this listener - it is called when the Up button is clicked.
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); // or any other code you want to run here
}
});
Related
Hello I am using this AppCompatDelegate to change between day/night themes
I have 2 activities A& B
this code called from activity B
it should recreating activity B & A with the chosen style
here is my code
applyNight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isNight) {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
I tested it on android 7 & 6 it's working fine i.e when changing mode in activity B and press back activity A recreating with the new theme.
When trying it on android 9 it's changing the activity B only and not affecting it's parent activity A.
I was having that problem too, and then took the advice of Chris Banes in Google's official Android Developers blog https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94 to set setDefaultNightMode in the app's application class in the first place, so I created a class EcwgApplication extending Application as he shows, and added android:name=".EcwgApplication" in the application section of the manifest. I also put my method for switching themes in the application class as well, that my settings activity can call when the user changes the theme setting (in addition to updating SharedPreferences with the change before calling it), so it looks like this:
public class EcwgApplication extends Application {
public void onCreate() {
super.onCreate();
int selectedDarkLightTheme = PreferenceManager.getDefaultSharedPreferences(this).getInt(getString(R.string.preferences_dark_light_mode_selected_key), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
public static void setDarkLightTheme(int selectedDarkLightTheme) {
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
}
This worked fine with Android OS versions 24 through 29, but with 21 (the lowest version this app is supporting) through 23 I would get a black screen on returning to the first activity, and while rotating the screen would fix that, it also made clear that UI state was not being saved. So I changed the StartActivity for the Settings screen to StartActivityForResult, and in onActivityResult, check if the version number <= 23, and if so, do this.recreate().
I need to keep doing more testing, but at least so far everything seems to be working great.
I've followed the example pattern for handling the android back button in the react-native docs and it works well. I can use the hardware back button to pop my navigation stack.
At the point that there's only 1 view in the stack though I don't pop it (just like the example), and I return false from my hardwareBackPress event listener. At this point it I see the componentWillUnmount method being called in my final view, at which point my app shuts down.
If I return true then nothing happens at all obviously.
What I want to happen is that the app merely gets "backgrounded" instead of exiting completely.
Answered my own question. The trick is to override the default back button behaviour in the MainActiviy:
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "foo";
}
#Override
public void invokeDefaultOnBackPressed() {
// do not call super. invokeDefaultOnBackPressed() as it will close the app. Instead lets just put it in the background.
moveTaskToBack(true);
}
}
Though I may be very late in giving the answer it may help other facing the issue.
Recently I came across the same requirement where I have to move the app to the background. I tried the solution provided by #pomo. Though it worked I faced problems. Sometimes on multiple clicking of the back button, the app misbehaves in android though it worked perfectly fine in iOS.
And then I came across the following issues in GitHub where it mentions the reason for the misbehaviour.
The following solution works perfectly fine now.
// android/app/.../MainActivity.java
#Override
public void invokeDefaultOnBackPressed() {
moveTaskToBack(true);
}
<!-- AndroidManifest.xml -->
<activity
...
android:launchMode="singleTop">
Link from where I get the solution
I hope I'm able to help guys with the same requirement.
I'm using this template https://github.com/kanytu/android-material-drawer-template just to try out material design so I've implemented a few fragments some have webviews some have not.
My problem is when switching between the fragments I can see them being successfully added to the backstack
getFragmentManager().beginTransaction().replace(R.id.container, new FAQ()).addToBackStack("FAQ").commit();
But when I press the back button it just closes the app.
When I change it to use Activity instead of ActionBarActivity the navigation works fine but I lose some other functionality.
There is an override on the back button
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
but even if that's removed it still happens. I think the problem lies somewhere in the super.onBackPressed
Is there any reason ActionBarActivity would break the back button?
I recently read a post about this, sorry I can't find it anymore... But basically, it explained that the primary function of the back button is to finish the current Activity.
In fact, according to the onBackPressed() official documentation:
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
And it would appear that even though the back button used to pop the backstack before 5.0, Google would have changed this behaviour with the new ActionBarActivity.
For my part, I used some workarround that works for me but that might not work for everybody, depending on your navigation implementation.
But in case it could be helpful to somebody, here it is :
#Override
public void onBackPressed()
{
if (mDrawerLayout.isDrawerOpen()) {
mDrawerLayout.closeDrawer();
} else if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
This way, ActionBarActivity.onBackPressed() is only called when the backstack is empty, in which case it destroys the ActionBarActivity.
You should check "getFragmentManager" & "getSupportFragmentManager" is matched your activity & actionbaractivity or not.
Because, in Activity:
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
in FragmentActivity:
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
We can see the same code which already handled pop fragments backstatck.
In my situation, I used actionbaractivity(extends FragmentAcvitiy), but I also used "getFragmentManager" , so I got the same error as you. After I have replaced "getFragmentManager" to "getSupportFragmentManager", that's ok! You also can replace "actionbaractiviy" to "Activity" to fix this problem.
Must ensure "getFragmentManager" match "Activity", "getSupportFragmentManager" match "FragmentActivity(ActionbarActivity)".
If you want add actionbar On API level 11 or higher, You can see below:
https://developer.android.com/guide/topics/ui/actionbar.html#Adding
On API level 11 or higher
The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.
I am using ActionBarSherlock on Android 4.0.3, so it might use the native ActionBar.
When I launch my application everything works fine. However, when I go to the Homescreen and wait until its killed (or simply change the system font, then it happens imediately) and then switch by the "last used"-dialog again to the app everything reloads smoothly, except the Actionbar has now empty tabs.
So the tabs are there, but empty (and do not work).
The strange thing is that even in the Application object onCreate is called (as in the TabParentActivity, see code below), so theoretically the Application should have been completely restarted (and not just partially like onResume...).
When I then kill my app (via the task manager) and reopen it the problem has gone.
How I add the Actionbar in my TabParentActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_parent);
//Global initialization
...
ActionBar ab = getSupportActionBar();
// set defaults for logo & home up
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayUseLogoEnabled(true);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
...
for(StolScreen s: screensInTabs){
Tab t = mAb.newTab().setText(s.displayName);
t.setTabListener(new NormalTabListener(this.mActivity, s));
mAb.addTab(t);
}
}
How it looks like:
All ok
Now tabs empty
I was now able to solve it by myself.
The reason is really crazy: In the above code I set the label of a tab in the loop with the s.displayName. s is belonging to an Enum called StolScreen.
In there the displayName is initialized via a call in Tools (initialized before), which retrieves the display name from an xml file. What was now actually happening when I was returning to the activity (and only then), was that StolScreen was loaded (in an Enum the fields are loaded like static members) BEFORE Tools was initialized.
So just an empty String was put on the tabs :D. Anyway, thx 4 help ;)
I have this problem with the Android ActionBarCompat project: On emulators with Android 4.0 the click on the app icon doesn't cause any onOptionsItemSelected event, whereas it works on all other OS versions.
Any input is greatly appreciated!
Are you seeing any touch feedback from the app icon? (Does it glow when you press it?)
Since many activities do not use the action bar home button, in apps that target API 14+ running on Android 4.0 it is disabled by default. (This is so that users don't try pressing it, see it glow, and wonder why nothing happened.) Apps that want to use this should call ActionBar#setHomeButtonEnabled(true).
We should probably revise the ActionBarCompat sample to surface this more clearly. One simple way to get you up and running would be to modify ActionBarHelperICS.java and add the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity.getActionBar().setHomeButtonEnabled(true);
}
In an app where you want more control over turning this on and off you would want to make further changes.
I had this problem as well. This code did the trick for me:
public void onCreate(Bundle savedInstanceState) {
...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//noinspection ConstantConditions
getActionBar().setHomeButtonEnabled(true);
} else {
getSupportActionBar().setHomeButtonEnabled(true);
}
}
Some extra info: minSdkVersion="7" targetSdkVersion="18". This is the LAUNCHER activity of my project, so it has no parent activity. Using setDisplayHomeAsUpEnabled(true) in other activities worked just fine.