How does one deep link into the preferences UI - android

I have an Android app that has used Preferences UI. I want to be able to deep link into the specific preference option. I can't figure out how to do that.
I have set up the preferences using the tutorial as follows
https://developer.android.com/guide/topics/ui/settings
https://medium.com/#JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c (Part 1)
https://medium.com/#JakobUlbrich/building-a-settings-screen-for-android-part-2-2ba63e2d7d1d
https://medium.com/#JakobUlbrich/building-a-settings-screen-for-android-part-3-ae9793fd31ec
I have set up deep linking activity using this tutorial
https://developer.android.com/training/app-links/deep-linking
I want to be able to deep link directly into the one of the preferences.
public class SettingsActivity extends AppCompatActivity
implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
FragmentFactory fragmentFactory = new AppFragmentFactory();
final FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager != null)
{
if (fragmentManager.findFragmentByTag(ROOT_FRAGMENT_TAG) == null)
{
final int containerId = R.id.detail_container;
final Fragment fragment = fragmentFactory.create(RootPreferencesFragment.class);
fragment.setArguments(savedInstanceState);
fragmentManager.beginTransaction()
.add(containerId, fragment, ROOT_FRAGMENT_TAG)
.commit();
}
}
}
}
#Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat callingFragment, Preference preference)
{
final Bundle arguments = preference.getExtras();
final Fragment fragment = fragmentFactory.create(getClassLoader(), preference.getFragment());
fragment.setArguments(arguments);
fragment.setTargetFragment(callingFragment, 0);
getSupportFragmentManager().beginTransaction()
.replace(R.id.detail_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.setBreadCrumbTitle(preference.getTitle())
.addToBackStack(preference.getKey())
.commit();
return true;
}
RootPreferencesFragment
public class RootPreferencesFragment extends PreferenceFragmentCompat implements DialogFragmentCallback
{
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
final FragmentActivity activity = Objects.requireNonNull(getActivity());
// Notifications
final Preference notificationsPreference = preferencesFactory.create(
accountCategory,
Preference.class,
RootViewModel.ACCOUNT_NOTIFICATIONS_PREFERENCE_KEY,
R.string.PushNotifications,
RootViewModel.ACCOUNT_CATEGORY_KEY);
notificationsPreference.setFragment(NotificationPreferencesFragment.class.getName());
// General Category
final PreferenceCategory generalCategory = preferencesFactory.create(
preferenceScreen,
PreferenceCategory.class,
RootViewModel.GENERAL_CATEGORY_KEY,
R.string.preferences_general_group);
// About category
final PreferenceCategory aboutCategory = preferencesFactory.create(
preferenceScreen,
PreferenceCategory.class,
RootViewModel.ABOUT_CATEGORY_KEY,
R.string.preferences_about_group);
// About
final Preference aboutPreference = preferencesFactory.create(
aboutCategory,
Preference.class,
RootViewModel.ABOUT_PREFERENCE_KEY,
R.string.preferences_about);
aboutPreference.setSummary(aboutViewModel.getAppVersion());
aboutPreference.setFragment(AboutPreferencesFragment.class.getName());
}
}
I don't know how to trigger deep link into a specific NotificationsPreference. There is only one activity, but there are three preferences.

Related

How to move from an activity to a fragment in Android?

May you assist by telling me the best way to moves from an activity to a fragment. This is what I have so far but it doesnt seem to work.
This is how I am calling the function(getCategory)
private void selectItem( int group, int usage)
{
if (!shown) return;
classificationGroup = group;
((DashboardActivity)getActivity()).getCallCategory(classificationGroup);
}
And in the activity I am trying to move to a fragment
public Fragment getCallCategory(int position) {
return new CallLogsFragment();
}
The standard pattern for creating fragments looks like this:
Inside your fragment class (make sure to import the android.support.v4.app.Fragment):
public class MyFragment extends Fragment {
public static final String TAG = "MyFragment";
private int position;
// You can add other parameters here
public static MyFragment newInstance(int position) {
Bundle args = new Bundle();
// Pass all the parameters to your bundle
args.putInt("pos", position);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.position = getArguments().getInt("pos");
}
}
Inside your activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add your parameters
MyFragment fragment = MyFragment.newInstance(10);
// R.id.container - the id of a view that will hold your fragment; usually a FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.container, fragment, MyFragment.TAG)
.commit();
}
}
When you want to access public methods of your fragment instance, use FragmentManager#findFragmentByTag(String tag) to find your instance of fragment:
MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag(MyFragment.TAG);
if(fragment != null){
// Do something with fragment
}
For a more detailed explanation, I suggest you read the official docs on fragments: https://developer.android.com/guide/components/fragments.html

Inner PreferenceScreen does not open with PreferenceFragmentCompat

My inner PreferenceScreen of PreferenceFragmentCompat is not showing, or seems to ignore tapping events.
I created MyPreferenceFragment that extends PreferenceFragmentCompat
public class MyPreferenceFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
}
then I changed my theme at styles.xml like
<style name="AppTheme" parent="#style/Theme.AppCompat.Light">
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
And finally create my preferences.xml file like
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="Check Me"/>
<PreferenceScreen android:title="My Screen"> <!-- This is not opening -->
<EditTextPreference android:title="Edit text" />
</PreferenceScreen>
</PreferenceScreen>
At the build.gradle I have added both:
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:preference-v7:23.0.1'
code of the Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:name="com.mando.preferenceapp.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Testing the above code I cannot open / get into the preference screen. Am I missing something? Why this isn't working?
After spending many many hours with tries, searching and thankfully with some assistance from the creators of the support library. I've managed to make it work.
Step 1. Activity
public class MyActivity extends AppCompatActivity implements
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// Create the fragment only when the activity is created for the first time.
// ie. not after orientation changes
Fragment fragment = getSupportFragmentManager().findFragmentByTag(MyPreferenceFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new MyPreferenceFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment, MyPreferenceFragment.FRAGMENT_TAG);
ft.commit();
}
}
#Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MyPreferenceFragment fragment = new MyPreferenceFragment();
Bundle args = new Bundle();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
ft.replace(R.id.fragment_container, fragment, preferenceScreen.getKey());
ft.addToBackStack(preferenceScreen.getKey());
ft.commit();
return true;
}
}
Tips.
Do not add the fragment by xml you will have crashes on orientation changes.
Handle the recreations of activity / fragment add in onCreate so as to avoid losing your fragment when inside a preference screen.
The host activity of the fragment should implement the PreferenceFragmentCompat.OnPreferenceStartScreenCallback and recreate fragments of the same instance.
Step 2. PreferenceFragment
public class MyPreferenceFragment extends PreferenceFragmentCompat {
public static final String FRAGMENT_TAG = "my_preference_fragment";
public MyPreferenceFragment() {
}
#Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
Tips.
Use the method setPreferencesFromResource and take advantage of the rootKey of each screen. This way your code will be reused properly.
Keep in mind that if you have code like findPreference in your fragment it should have null checks as when you were in inner screens this will give you nothing.
The thing that is missing now is the implementation of the back arrow in the actionbar (home action) but this never works by itself ;-)
I' also created a demo app wrapping all this code you can find it on github.
Solution is to start another fragment of the same class but with different root key. No Activity actions involved.
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey){
if(getArguments() != null){
String key = getArguments().getString("rootKey");
setPreferencesFromResource(R.xml.preferences, key);
}else{
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
#Override
public void onNavigateToScreen(PreferenceScreen preferenceScreen){
ApplicationPreferencesFragment applicationPreferencesFragment = new ApplicationPreferencesFragment();
Bundle args = new Bundle();
args.putString("rootKey", preferenceScreen.getKey());
applicationPreferencesFragment.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(getId(), applicationPreferencesFragment)
.addToBackStack(null)
.commit();
}
I did it slightly differently, I'm launching a new activity for each screen. This seems to require less hacks: no need to mess with swapping fragments and background colors. You also get activity change animation as a bonus!
public class PreferencesActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
final static private String KEY = "key";
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
if (savedInstanceState != null)
return;
Fragment p = new PreferencesFragment();
String key = getIntent().getStringExtra(KEY);
if (key != null) {
Bundle args = new Bundle();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, key);
p.setArguments(args);
}
getSupportFragmentManager().beginTransaction()
.add(R.id.preferences, p, null)
.commit();
}
#Override public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat, PreferenceScreen preferenceScreen) {
Intent intent = new Intent(PreferencesActivity.this, PreferencesActivity.class);
intent.putExtra(KEY, preferenceScreen.getKey());
startActivity(intent);
return true;
}
#Override public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PreferencesFragment extends PreferenceFragmentCompat implements ... {
private static final String FRAGMENT_DIALOG_TAG = "android.support.v7.preference.PreferenceFragment.DIALOG";
private String key;
#Override public void onCreatePreferences(Bundle bundle, String key) {
setPreferencesFromResource(R.xml.preferences, this.key = key);
}
// this only sets the title of the action bar
#Override public void onActivityCreated(Bundle savedInstanceState) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) actionBar.setTitle((key == null) ? "Settings" : findPreference(key).getTitle());
super.onActivityCreated(savedInstanceState);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:orientation="vertical"
android:padding="0dp"
android:id="#+id/preferences">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- preference fragment will be inserted here programmatically -->
</LinearLayout>
Another solution is to track the preference screens yourself and use the PreferenceFragmentCompat api
Here's the basic solution. (It doesn't cover all the edge cases, see advanced solution below)
Ensure you have configChanges="orientation" to prevent create/destroy
<activity
android:name=".MyPreferencesActivity"
android:configChanges="orientation" />
In the Activity you want to keep a Stack of PreferenceScreens and push/pop as needed
/* track the screens as a Stack */
private Stack<PreferenceScreen> preferenceScreens = new Stack<>();
// ensure your Activity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback
#Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat, PreferenceScreen preferenceScreen) {
preferenceScreens.push(preferenceFragmentCompat.getPreferenceScreen());
preferenceFragmentCompat.setPreferenceScreen(preferenceScreen);
return true;
}
#Override
public void onBackPressed() {
if (preferenceScreens.empty()) {
super.onBackPressed();
} else {
prefsFragment.setPreferenceScreen(preferenceScreens.pop());
}
}
Optional: In your Fragment that extends PreferenceFragmentCompat, add setRetainInstance(true). (Note that without
this it will likely work also, but it 'could' break occasionally. If you set 'Don't keep Activities' to true, and
you'll see that it will get collected)
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setRetainInstance(true);
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey);
...
That's it! Except that if you want to cover edge cases...
Advanced Solution (If you set 'Don't Keep Activities to True, you'll need to ensure you can rebuild everything from savedInstanceState)
Note that the accepted answer doesn't actually preserve state.
set 'Don't Keep Activities' to True
navigate to a nested PreferenceScreen
Press home and then navigate back to the app
It 'should' still be on the Nested PreferenceScreen, but it's actually on the root one
Full Advanced Solution using PreferenceFragmentCompat api and preserving the PreferenceScreen stack
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Stack;
/**
* Class to Show the preference screen with Activity keeping state
* #author Aaron Vargas
*/
public class MyPreferencesActivityStateful extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
private static final String PREFERENCE_SCREENS = "PREFERENCE_SCREENS";
private PrefsFragment prefsFragment;
private Stack<PreferenceScreen> preferenceScreens = new Stack<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content. Re-Use if possible
String tag = PrefsFragment.class.getName();
prefsFragment = (PrefsFragment) getSupportFragmentManager().findFragmentByTag(tag);
if (prefsFragment == null) prefsFragment = new PrefsFragment();
getSupportFragmentManager().beginTransaction().replace(android.R.id.content,
prefsFragment, tag).commit();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// rebuild preferenceScreen stack
for (String screenKey : Objects.requireNonNull(savedInstanceState.getStringArrayList(PREFERENCE_SCREENS))) {
preferenceScreens.push((PreferenceScreen) prefsFragment.findPreference(screenKey));
}
PreferenceScreen preferenceScreen = preferenceScreens.pop();
if (preferenceScreen != prefsFragment.getPreferenceScreen()) { // optimize if same
prefsFragment.setPreferenceScreen(preferenceScreen);
}
}
#Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat, PreferenceScreen preferenceScreen) {
preferenceScreens.push(preferenceFragmentCompat.getPreferenceScreen());
preferenceFragmentCompat.setPreferenceScreen(preferenceScreen);
return true;
}
#Override
public void onBackPressed() {
// account for onRestore not getting called equally to onSave
while (preferenceScreens.contains(prefsFragment.getPreferenceScreen())) {
preferenceScreens.remove(prefsFragment.getPreferenceScreen());
}
if (preferenceScreens.empty()) {
super.onBackPressed();
} else {
prefsFragment.setPreferenceScreen(preferenceScreens.pop());
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
preferenceScreens.push(prefsFragment.getPreferenceScreen());
ArrayList<String> keys = new ArrayList<>(preferenceScreens.size());
for (PreferenceScreen screen : preferenceScreens) {
keys.add(screen.getKey());
}
outState.putStringArrayList(PREFERENCE_SCREENS, keys);
}
public static class PrefsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setRetainInstance(true); // ensure in manifest - android:configChanges="orientation"
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
}
You can also handle all this in your Fragment instead of the Activity. Here's a gist of that https://gist.github.com/aaronvargas/0f210ad8643b512efda4acfd524e1232
Using Navigation Component (Android Jetpack) and Kotlin it's very easy now:
class PrefsFragment : PreferenceFragmentCompat() {
private val args: PrefsFragmentArgs by navArgs()
override fun onCreatePreferences(state: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.prefs, args.rootKey)
}
override fun onNavigateToScreen(preferenceScreen: PreferenceScreen?) {
findNavController().navigate(
PrefsFragmentDirections.changeRoot(preferenceScreen!!.key)
)
}
}
Based on #squirrel Intent solution, I made it work this way. It requires even less hacking.
Activity:
import android.support.v7.app.AppCompatActivity;
public class SettingsActivity extends AppCompatActivity {
public static final String TARGET_SETTING_PAGE = "target";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragment = new SettingsFragment();
Intent intent = getIntent();
if (intent != null) {
String rootKey = intent.getStringExtra(TARGET_SETTING_PAGE);
if (rootKey != null) {
settingsFragment.setArguments(Bundler.single(TARGET_SETTING_PAGE, rootKey));
}
}
getFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();
}
}
Fragment:
import android.support.v14.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
if (arguments != null && arguments.getString(TARGET_SETTING_PAGE) != null) {
setPreferencesFromResource(R.xml.preferences, arguments.getString(TARGET_SETTING_PAGE));
} else {
addPreferencesFromResource(R.xml.preferences);
}
}
#Override
public void onNavigateToScreen(PreferenceScreen preferenceScreen) {
Intent intent = new Intent(getActivity(), SettingsActivity.class)
.putExtra(TARGET_SETTING_PAGE, preferenceScreen.getKey());
startActivity(intent);
super.onNavigateToScreen(preferenceScreen);
}
}
It is sad you need so much hacks in the support appcompat libraries for something that works flawlessly out-of-the-box in standard android.
Alternative using Navigation component + androidx.appcomat:
https://stackoverflow.com/a/59732509/5437789
With this, you wont loose the back stack and go back to main page settings when you press back button.
Here is a simple solution from android documentation. To implement inner preference screen navigation with PreferenceFragmentCompact all you have to do is add fragment attribute to the embedded preference screen giving the fragment full path to navigate to eg. com.example.FragmentName.
Sample code:
<PreferenceCategory app:title="#string/choose_theme"
android:icon="#drawable/ic_baseline_color_lens_24">
<SwitchPreference
android:title="#string/apply_night_mode"
android:key="#string/key_enable_night_mode"/>
<PreferenceScreen
android:fragment="com.example.simbokeyboard.BlankFragment"
android:title="Custom Theme"
android:summary="#string/theme_summary">
<Preference
android:key="#string/choose_theme"
android:title="#string/choose_theme"
android:layout="#layout/theme_chooser"/>
</PreferenceScreen>
</PreferenceCategory>

Reloading PreferenceFragment when new language selected in preferences

In my app's preferences I have an option to change app's language.
public class Fragment_Preferences extends PreferenceFragment {
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
SharedPreferences preferences;
#Override
public void onCreate(Bundle savedInstanceState) {
preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("language_preference"))
{
// Set language change flag to true -
// the Main Fragment will be recreate when this fragment finishes and the main restarts
Common_Methods.set_locale_changed(true);
}
}
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
}
So when I change the language, the preference fragment doesn't change it's language immediately. I have to exit preferences, then in my Fragment or Activity (depends from where I called for Preference fragment) I have this code, which restarts the current fragment or activity with new language settings:
public void onConfigurationChanged(Configuration newConfig) {
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
{
finish();
startActivity(getIntent());
} else {recreate();}
}
}, 1);
Common_Methods.set_locale_changed(false); // Reset the Language change flag to prevent repeating Fragment recreation.
super.onConfigurationChanged(newConfig);
}
If I reopen Preference fragment at that point - it will be in the new selected language.
I tried to copy the above method to my Preference_Fragment, but I'm getting errors. So the question is: how can I recreate/reload the Preference_Fragment with the new language immediately after it was selected, without having to exit the fragment first?
It is best if you let the activity know (through getActivity() and casting) that you want your fragment updated, and add logic to the activity to remove the fragment and add a new instance.
In your settings activity:
public void restartFragment() {
SettingsFragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
}
And in your settings fragment:
((SettingsActivity) getActivity()).restartFragment();
Floren's answer didn't work for me (maybe due to using the Android Support Library), but it lead me to the following solution based on the same basic idea from Florens. The difference is removing and adding the fragment instead of replacing:
In your activity:
public void restartFragment() {
FragmentManager fm = getSupportFragmentManager();
if (fragment != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
fragment = <new instance>;
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentActivity, main, "Main");
ft.commit();
}
In your fragment:
((<your activity>)getActivity()).restartFragment();

Android Fragments not calls a Backstack

I have a class "bancoActivity" that extends Fragment implements ActionBar.TabListener that calls another class "pagamentos" extends Fragment implements ActionBar.TabListener.
When I'm in class "pagamentos" and click on the physical button "back" nothing happens, and when i click again the application finish.
I leave there my code so that your can analyze.
Obrigado.
part of the bancoActivity:
#Override
public void onItemClick(AdapterView<?> customviewadapter, View view, int position, long id) {
listViewItem item = items.get(position);
String Titulo = item.Title;
if(Titulo.equals("Pagamentos")) {
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
pagamentos fragment2 = new pagamentos();
fragmentTransaction2.hide(bancoActivity.this);
fragmentTransaction2.add(android.R.id.content, fragment2);
fragmentTransaction2.addToBackStack("banco");
fragmentTransaction2.commit();
}
}
part of the pagamentos:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.pagamentos);
FragmentManager fm = getFragmentManager();
the two activities extends and implements:
public class pagamentos extends Fragment implements ActionBar.TabListener{
public class bancoActivity extends Fragment implements ActionBar.TabListener
I'm a newbie too, but I'll take a stab at it...
I think you need to instantiate both fragments from the activity that holds them, and use 1 fragment manager to do the switching. Its hard for me to tell without your whole code, but I have a similar setup working, here is how:
public class MainActivity extends Activity implements ActionBar.TabListener {
public static final int SEARCH_FRAG = 1;
public static final int MAP_FRAG = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragMan = (FragmentManager) getFragmentManager();
mapFrag = ExtendedMapFragment.newInstance();
searchFrag = SearchFragment.newInstance();
if (mapShown == false) {
swapFrags(SEARCH_FRAG);
} else {
swapFrags(MAP_FRAG);
}
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
searchTab = actionBar.newTab()
.setText(R.string.search_tab_label)
.setTabListener(this);
actionBar.addTab(searchTab);
mapTab = actionBar.newTab()
.setText(R.string.map_tab_label)
.setTabListener(this);
actionBar.addTab(mapTab);
if (savedInstanceState != null) {
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("currentTab"));
}
actionBar = null;
}
public void swapFrags(int whatFrag) {
if (whatFrag == SEARCH_FRAG) {
//switch to frag 1, ie searchFrag
FragmentTransaction trans = fragMan.beginTransaction();
trans.replace(R.id.map, searchFrag);
mapShown = false;
trans.addToBackStack(null);
trans.commit();
}
if (whatFrag == MAP_FRAG) {
//switch to frag 2, ie mapFrag
if (lbServ != null) {
update.autoCenter(lbServ.getCurrentLatLng());
}
FragmentTransaction trans = fragMan.beginTransaction();
trans.replace(R.id.map, mapFrag);
mapShown = true;
trans.addToBackStack(null);
trans.commit();
}
}
}
The swapFrags() method is also called from my onTabSelected callback. I think, since I only have one fragment manager, and the same manager is calling all the addToBackStack() methods, it is more organized. When I open the app, select a new tab, then hit physical back key, it goes back to the previous tab, which is what you are after, no?
One thing I found difficult to learn with fragments is that all calls, keys, etc. go to the activity first. Push a button on the fragment, the activity that holds it gets the callback first (whether it uses it or not), then if there is a listener in the fragment it will get the call also, but fragments can't do really anything outside themselves, and fragment transactions involve objects outside the fragment.
I suppose you could set up an interface between fragment and activity, with a method like swapFrags() in the activity, where the fragment can ask to be swapped, that should do it too I think.

Fragments onResume from back stack

I'm using the compatibility package to use Fragments with Android 2.2.
When using fragments, and adding transitions between them to the backstack, I'd like to achieve the same behavior of onResume of an activity, i.e., whenever a fragment is brought to "foreground" (visible to the user) after poping out of the backstack, I'd like some kind of callback to be activated within the fragment (to perform certain changes on a shared UI resource, for instance).
I saw that there is no built in callback within the fragment framework. is there s a good practice in order to achieve this?
For a lack of a better solution, I got this working for me:
Assume I have 1 activity (MyActivity) and few fragments that replaces each other (only one is visible at a time).
In MyActivity, add this listener:
getSupportFragmentManager().addOnBackStackChangedListener(getListener());
(As you can see I'm using the compatibility package).
getListener implementation:
private OnBackStackChangedListener getListener()
{
OnBackStackChangedListener result = new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
FragmentManager manager = getSupportFragmentManager();
if (manager != null)
{
MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem);
currFrag.onFragmentResume();
}
}
};
return result;
}
MyFragment.onFragmentResume() will be called after a "Back" is pressed. few caveats though:
It assumes you added all
transactions to the backstack (using
FragmentTransaction.addToBackStack())
It will be activated upon each stack
change (you can store other stuff in
the back stack such as animation) so
you might get multiple calls for the
same instance of fragment.
I've changed the suggested solution a little bit. Works better for me like that:
private OnBackStackChangedListener getListener() {
OnBackStackChangedListener result = new OnBackStackChangedListener() {
public void onBackStackChanged() {
FragmentManager manager = getSupportFragmentManager();
if (manager != null) {
int backStackEntryCount = manager.getBackStackEntryCount();
if (backStackEntryCount == 0) {
finish();
}
Fragment fragment = manager.getFragments()
.get(backStackEntryCount - 1);
fragment.onResume();
}
}
};
return result;
}
After a popStackBack() you can use the following callback : onHiddenChanged(boolean hidden) within your fragment
The following section at Android Developers describes a communication mechanism Creating event callbacks to the activity. To quote a line from it:
A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary.
Edit:
The fragment has an onStart(...) which is invoked when the fragment is visible to the user. Similarly an onResume(...) when visible and actively running. These are tied to their activity counterparts.
In short: use onResume()
If a fragment is put on backstack, Android simply destroys its view. The fragment instance itself is not killed. A simple way to start should to to listen to the onViewCreated event, an put you "onResume()" logic there.
boolean fragmentAlreadyLoaded = false;
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState == null && !fragmentAlreadyLoaded) {
fragmentAlreadyLoaded = true;
// Code placed here will be executed once
}
//Code placed here will be executed even when the fragment comes from backstack
}
In my activity onCreate()
getSupportFragmentManager().addOnBackStackChangedListener(getListener());
Use this method to catch specific Fragment and call onResume()
private FragmentManager.OnBackStackChangedListener getListener()
{
FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener()
{
public void onBackStackChanged()
{
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (currentFragment instanceof YOURFRAGMENT) {
currentFragment.onResume();
}
}
};
return result;
}
A little improved and wrapped into a manager solution.
Things to keep in mind. FragmentManager is not a singleton, it manages only Fragments within Activity, so in every activity it will be new. Also, this solution so far doesn't take ViewPager into account that calls setUserVisibleHint() method helping to control visiblity of Fragments.
Feel free to use following classes when dealing with this issue (uses Dagger2 injection). Call in Activity:
//inject FragmentBackstackStateManager instance to myFragmentBackstackStateManager
FragmentManager fragmentManager = getSupportFragmentManager();
myFragmentBackstackStateManager.apply(fragmentManager);
FragmentBackstackStateManager.java:
#Singleton
public class FragmentBackstackStateManager {
private FragmentManager fragmentManager;
#Inject
public FragmentBackstackStateManager() {
}
private BackstackCallback backstackCallbackImpl = new BackstackCallback() {
#Override
public void onFragmentPushed(Fragment parentFragment) {
parentFragment.onPause();
}
#Override
public void onFragmentPopped(Fragment parentFragment) {
parentFragment.onResume();
}
};
public FragmentBackstackChangeListenerImpl getListener() {
return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl);
}
public void apply(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
fragmentManager.addOnBackStackChangedListener(getListener());
}
}
FragmentBackstackChangeListenerImpl.java:
public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener {
private int lastBackStackEntryCount = 0;
private final FragmentManager fragmentManager;
private final BackstackCallback backstackChangeListener;
public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) {
this.fragmentManager = fragmentManager;
this.backstackChangeListener = backstackChangeListener;
lastBackStackEntryCount = fragmentManager.getBackStackEntryCount();
}
private boolean wasPushed(int backStackEntryCount) {
return lastBackStackEntryCount < backStackEntryCount;
}
private boolean wasPopped(int backStackEntryCount) {
return lastBackStackEntryCount > backStackEntryCount;
}
private boolean haveFragments() {
List<Fragment> fragmentList = fragmentManager.getFragments();
return fragmentList != null && !fragmentList.isEmpty();
}
/**
* If we push a fragment to backstack then parent would be the one before => size - 2
* If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2
*
* #return fragment that is parent to the one that is pushed to or popped from back stack
*/
private Fragment getParentFragment() {
List<Fragment> fragmentList = fragmentManager.getFragments();
return fragmentList.get(Math.max(0, fragmentList.size() - 2));
}
#Override
public void onBackStackChanged() {
int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount();
if (haveFragments()) {
Fragment parentFragment = getParentFragment();
//will be null if was just popped and was last in the stack
if (parentFragment != null) {
if (wasPushed(currentBackStackEntryCount)) {
backstackChangeListener.onFragmentPushed(parentFragment);
} else if (wasPopped(currentBackStackEntryCount)) {
backstackChangeListener.onFragmentPopped(parentFragment);
}
}
}
lastBackStackEntryCount = currentBackStackEntryCount;
}
}
BackstackCallback.java:
public interface BackstackCallback {
void onFragmentPushed(Fragment parentFragment);
void onFragmentPopped(Fragment parentFragment);
}
This is the correct answer you can call onResume() providing the fragment is attached to the activity. Alternatively you can use onAttach and onDetach
onResume() for the fragment works fine...
public class listBook extends Fragment {
private String listbook_last_subtitle;
...
#Override
public void onCreate(Bundle savedInstanceState) {
String thisFragSubtitle = (String) getActivity().getActionBar().getSubtitle();
listbook_last_subtitle = thisFragSubtitle;
}
...
#Override
public void onResume(){
super.onResume();
getActivity().getActionBar().setSubtitle(listbook_last_subtitle);
}
...
public abstract class RootFragment extends Fragment implements OnBackPressListener {
#Override
public boolean onBackPressed() {
return new BackPressImpl(this).onBackPressed();
}
public abstract void OnRefreshUI();
}
public class BackPressImpl implements OnBackPressListener {
private Fragment parentFragment;
public BackPressImpl(Fragment parentFragment) {
this.parentFragment = parentFragment;
}
#Override
public boolean onBackPressed() {
((RootFragment) parentFragment).OnRefreshUI();
}
}
and final extent your Frament from RootFragment to see effect
My workaround is to get the current title of the actionbar in the Fragment before setting it to the new title. This way, once the Fragment is popped, I can change back to that title.
#Override
public void onResume() {
super.onResume();
// Get/Backup current title
mTitle = ((ActionBarActivity) getActivity()).getSupportActionBar()
.getTitle();
// Set new title
((ActionBarActivity) getActivity()).getSupportActionBar()
.setTitle(R.string.this_fragment_title);
}
#Override
public void onDestroy() {
// Set title back
((ActionBarActivity) getActivity()).getSupportActionBar()
.setTitle(mTitle);
super.onDestroy();
}
I have used enum FragmentTags to define all my fragment classes.
TAG_FOR_FRAGMENT_A(A.class),
TAG_FOR_FRAGMENT_B(B.class),
TAG_FOR_FRAGMENT_C(C.class)
pass FragmentTags.TAG_FOR_FRAGMENT_A.name() as fragment tag.
and now on
#Override
public void onBackPressed(){
FragmentManager fragmentManager = getFragmentManager();
Fragment current
= fragmentManager.findFragmentById(R.id.fragment_container);
FragmentTags fragmentTag = FragmentTags.valueOf(current.getTag());
switch(fragmentTag){
case TAG_FOR_FRAGMENT_A:
finish();
break;
case TAG_FOR_FRAGMENT_B:
fragmentManager.popBackStack();
break;
case default:
break;
}

Categories

Resources