How do I delay onCreateOptionsMenu(Menu menu)? - java

Im a creating and options menu dynamically, the option menu depends on data that is created during the onCreate() stage.
The problem is that onCreateOptionsMenu() is called before the data is created. Is there a way to execute after onCreate() ?
Thanks
Fabii

You can use invalidateOptionsMenu(), but it requires API level 11.
Also, you can choose to override onPrepareOptionsMenu() instead, which will give you to opportunity to modify the menu before it is displayed each time.

Related

How to hide MenuItem in the toolbar in certain situations?

I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I'm trying to call it like this:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
Here's the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. onPrepareOptionsMenu gets called every time the menu needs to be displayed (instead of once, during setup).
So you can set a boolean or whatever to say whether the item should be shown, and call invalidateOptionsMenu() to redisplay it. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item
Under what condition you want it to be hidden, you can add it under that condition.
menu.visibility = View.INVISIBLE
or
menu.visibility = View.GONE

Hiding MenuItem base on the Screen(Activity)

Possible way of hiding menuItem base on the screen(Activity) is in the foreground. I have four (4) menuItem and I want to show two (2) in the action bar and force 2 into the overflow menu, in some screen I want to show three (3) and have one (1) in the overflow menu,and in some screen the page tile is long I do not want it truncated but instead I want to show only one (1) menuItem in the action bar and force the other three (3) into the overflow menu.
I need a generic way to do this.
I need to do this programmatically I don't need the xml answer of IfRoom, I have a BaseActivity which extends ActionBar and I have BaseActivityHelper where I have my menu layout inflated, all my other activity extends the BaseActivity.
I would love to share my code but I'm not allowed to do so, the big question is if you are the one faced with this situation how will you do it.
The docs say:
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)
So, you can grab your menu item in onPrepareOptionsMenu and call it's setShowAsAction(int actionEnum) with the appropriate option (SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER)

How to make a dialog but with the ability to set its content with a custom xml?

In my android app, I want to make a dialog box, the kind of one you get from alertbuilders, where you get access to set positive/neutral/negative button click, but I also want to use the .setcustomview() to load the content with my xml file.
Does android have a way to do this? I want to avoid making those buttons...
Thanks
I don't see a setcustomview() method for the AlertDialog.Builder class, but I do see a setView() method, if that's what you mean. :)
From my experience, using setView() will allow you to set your custom XML layout (though you will still need to inflate it first) without requiring you to recreate / add buttons to your layout. You just need to call the setXxxButton() methods before you build.

Ringtonepreference not triggering anything at all

I have a RingtonePreference nested inside 2 PreferenceScreen declared as:
<PreferenceScreen
//other preferences
<PreferenceScreen
//other preferences
<RingtonePreference
android:key="ringtone"
android:title="#string/ringtone_title"
android:summary="#string/sipringtone_ringtone_summary"
android:ringtoneType="ringtone" />
/>
/>
The PreferenceFragment is nested inside a ActivityGroup as such. The path to the fragment is as followed:
MainActivity(TabActivity)->Activity1(ActivityGroup)->Activity2(Activity)->PreferenceFragment
I did not write this code but am picking off where someone left off. The dialogs from clicking every preference was crashing the app because it did not like the context of Activity2. This was an ActivityGroup related issue that was resolved by forcing the context of each preference to the context of Activity1.
EditPreferences, ListPreferences, and CheckPreferences all worked as intended but RingtonePreference is giving me a lot of trouble. Although the dialog pops up to let me choose the selection of ringtones, it does not save the setting.
Selecting a ringtone from the list and pressing the OK button does not trigger onPreferenceChange() or onSharedPreferenceChanged(). I tried creating a custom RingtonePreference and overriding onSaveRingtone() but that did not get called at all. However, other methods like onPrepareRingtonePickerIntent() and onRestoreRingtone() did get called. I tried a bunch of other options that were mentioned on stack overflow but had no luck. I'm running out of ideas to get RingtonePreference to work and think starting my own ringtone picker using RingtoneManager is the best alternative. If anyone could give me some advise on how to get RingtonePreference to work then that'll be awesome.
Edit: I believe this is a context problem but I can't find out how to solve it yet.
I was unable to get the RingtonePreference to work but I did find some new details and an alternative. RingtonePreference looks like it starts a new activity for the dialog. If you have launchmode=singleInstance, that will mess up the RingtonePreference because you're starting that activity on a different Task stack. Using launchmode=singleTask or removing the launchmode could solve your problem, but not for me entirely (but I still needed launchmode to not be equal to singleInstance). My solution was to add the preference manually in java code. The steps were
1) Find your preference screen
2) Make a preference
3) Set your preference details, i.e. title & summary
4) (Optional) Arrange the order of your preference (google the setOrder function for preference)
5) Set the onPreferenceCLickerListener to the preference you created
6) Inside onPreferenceClick, Launch the ringtone picker dialog configured by RingtoneManager and start the activity from where ever you want.
7) Add the preference to your preference screen chosen in step 1.
8) On the activity/fragment where you started the ringtone picker, override on onActivityResult() and handle the chosen ringtone.
Good luck!
kyrax's answer seems appropriate, although I didn't want to go through all the mess of creating a Preference programmatically and then inserting it that way.
To solve this problem, I started off with a complete XML and then I simply added the OnPreferenceChangeListener to the RingtonePreference. This can be done from your PreferenceFragment:
Preference notificationSoundPref = findPreference("ringtone);
notificationSoundPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// do what you need here
return true;
}
});

onDestroy for making data persistent and sync with the server

I have a little problem to detect when the application is finished. I need to do some actions onDestroy like save the parameters into the database and make a final connection to the server.
The problem is that if I put the code in onDestroy its is called when the orientation changes for example. Putting
android:configChanges="orientation|keyboardHidden"
in the manifest for that activity the landscape/portrait layouts don't swap. And adding
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
Changes the layouts but the buttons and labels do not get the onClickListeners and the text labels correctly. How can I solve that? Thanks
The problem is that your layout items aren't initialized again because you're initializing them in your onCreate() function, and then you're disrupting them with a new layout in onConfigurationChanged().
One option is to move the initialization to a new function that gets called from both onCreate() and onConfigurationChanged().
Another option is to use the android:onclick="" (and related) attributes in your layout.
The option I would choose is different though. I would allow Android to manage orientation (and to call onDestroy()) and in onDestroy() I would install an Alarm for, say, 10 seconds (which I imagine is plenty of time to have onCreate() called again). In onCreate() I would cancel the alarm. When the alarm fires, I would perform my save actions.
Declare buttons and labels as class variable.
setContentView recreates your view, so you must rebind your data. the best approach would be a function called both from onCreate() and onConfigurationChanged(), with layout creation and bindings.
If you don't want to anything to happen when orientation changes occur, than you should not re-setContentView(). Basically you are telling your app: "DO NOTHING WHEN ORIENTATION CHANGES". So, remove the setContentView inside the onConfigurationChanged() or test for which orientation currently is active and then load desired layout resources.
When orientation changes onDestroy() is called because the changes restart your entire activity.
Read more here:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
http://developer.android.com/guide/topics/resources/providing-resources.html
Orientation testing:
Setting the background of an Activity
Could you do that stuff in overriden finish() of the activity?

Categories