Twentyonehundred
Twentyonehundred

Reputation: 2239

Managing content inside a fragment in Android

I have an app that consists of 3 fragments as tabs based upon an initial activity.

The middle tab has a button to save a date. This is just a date picker that adds the selected date to local preferences.

After setting this date, when I slide to the left fragment tab I want to see that date.

What do I need to change / consider in terms of fragment life-cycle or anything else so that when I update this value in the middle fragment tab, I can get, use and display the data on the leftmost fragment tab immediately.

Currently it only works when I close the app. Re-creating the fragment does not seem to get an updated date value. I've tried re-getting the data in onCreate, onStart, onResume, instantiateItem etc.

The tabs code is based off of this simple, official Google example project: https://developer.android.com/samples/SlidingTabsColors/index.html

I've not included any code yet as the concept can be easily replicated / understood from the example project.

        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();

Upvotes: 0

Views: 86

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199805

If you are using Shared Preferences, then you can create a OnSharedPreferenceChangeListener in your left fragment that listens for the date changing and updates its view appropriately.

Upvotes: 1

Related Questions