Comments (9)
@madlymad I used a normal Preference and set onPreferenceClick to launch Ringstone Select.
public boolean onPreferenceClick(Preference preference)
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
String path = settings.getString("alarma", "content://settings/system/notification_sound");
Uri uri = !TextUtils.isEmpty(path) ? Uri.parse(path) : null;
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
startActivityForResult(intent, 0);
}
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
if (resultCode != Activity.RESULT_OK )
return;
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
Editor editor = settings.edit();
editor.putString("alarma", uri == null ? "": uri.toString());
editor.commit();
}
from android-support-v4-preferencefragment.
The problem is related to this code in RingtonePreference:
PreferenceFragment owningFragment = getPreferenceManager().getFragment();
if (owningFragment != null) {
owningFragment.startActivityForResult(intent, mRequestCode);
} else {
getPreferenceManager().getActivity().startActivityForResult(intent, mRequestCode);
}
We have no owning fragment since PreferenceManager.setFragment() needs a PreferenceFragment and therefore the PreferenceManagerCompat can't set it.
But as you see in the above snippet, if no owner fragment is found the Activity is used instead to start ringtone picker. The workaround I use is to catch the result in my Activity like this:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
return;
}
Bundle bundle = data.getExtras();
if (bundle != null) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
String ringtoneString = "";
if (uri != null) {
ringtoneString = uri.toString();
}
sharedPrefs.edit().putString("yourRingtoneKey", ringtoneString).commit();
}
}
License
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/
from android-support-v4-preferencefragment.
Any workaround/fix for that?
from android-support-v4-preferencefragment.
@MiguelAngelLV thanks that worked!
Tip of the problem that may help (for patching at the lib):
After searching android grep code related to RingtonePreference
I found that the problem is that the onActivityResult
is initialized at the Activity
level that hosts your fragment so PreferenceFragment.class
or your extension never get the activity result callback in order to save the selected ringtone.
I tried to fix that in library level but with no success.
Workaround (much easier for me):
In your code though you can patch the problem by passing the activity result from your Activity
to your Fragment
that extends PreferenceFragment
.
-
at your xml you can use
RingtonePreference
-
at your
PreferencesActivity
override:@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); PreferenceActivityCallbacks callback = null; SherlockFragment f = this.getSupportFragmentManager().findFragmentByTag("your_fragment_tag"); if ( f instanceof PreferenceActivityCallbacks ) callback = (PreferenceActivityCallbacks) f; if (callback != null) { callback.onActivityResult(requestCode, resultCode, data); } }
-
PreferenceActivityCallbacks
is my interface that I use to pass data from activity to fragmentpublic interface PreferenceActivityCallbacks { // ... public void onActivityResult(int requestCode, int resultCode, Intent data); // ... }
-
You are ready, you do not have to override
onActivityResult
in your fragment just implement the interface
public class YourPreferenceFragment extends PreferenceFragment implements PreferenceActivityCallbacks {
from android-support-v4-preferencefragment.
hey @tbfr, thanks for your workaround! I would like to use it in an open source project (TextSecure). Could you please do us a favor and add a license (GPL, Apache?) to your code? I'm no lawyer but I would like to be on the save side ;)
from android-support-v4-preferencefragment.
Hey @agrajaghh!
Please feel free to use the code in any way you want :)
from android-support-v4-preferencefragment.
Just came across this same issue. Thanks @tbfr for the info. Looking into a bit further, you can simplify this by just delegating the onActivityResult call to the wrapped PreferenceFragment from your Activity. That, in turn, dispatches the call to the RingtonePreference that initiated the call to begin with.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(ID);
fragment.onActivityResult(requestCode, resultCode, data);
}
Hope that's helpful.
from android-support-v4-preferencefragment.
Thε easiest and hassle free solution would be to do something like this on your Activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment frag : fragments)
frag.onActivityResult(requestCode, resultCode, data);
}
}
That way the activity is going to forward the onActivityResult to all hosted fragments, without having to keep their references or ids or tags by yourself
from android-support-v4-preferencefragment.
@alexstyl Thanks man, super simple solution and its working with multiple RingtonPreferences in one Activity.
from android-support-v4-preferencefragment.
Related Issues (17)
- findPreference return null in api 10
- Push lib to Maven Central/jCenter HOT 25
- Big indents on Android L HOT 2
- NoClassDefFoundError R$layout HOT 1
- blank fragment HOT 9
- Plugin is too old HOT 1
- Can you add a license statement in your readme.md?
- This Handler class should be static or leaks might occur: Handler
- Listeners are not working? HOT 1
- cannot work with ListPreference
- Lov3
- Sample app code? HOT 6
- Put it on Maven Central HOT 2
- Memory leak when using soft keyboard HOT 3
- Context menu items are the wrong color when using a app theme besides Theme.AppCompat HOT 1
- not working with Preference Headers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-support-v4-preferencefragment.