Search code examples
androidandroid-fragmentsactionbarsherlockandroid-actionbar

Fragments not showing title after application is resumed?


I have an odd issue which I've never seen anywhere on SO so I've resorted to posting here, hoping I make it clear enough.

I have a simple SherlockFragmentActivity as shown further down which contains three fragments which all call getActivity().setTitle() in their onCreateOptionsMenu() allowing my app to change titles depending on which fragment is visible.

This works as desired, but for some reason (perhaps unrelated) when I exit my application by means of the HOME button occasionally the title isn't visible upon reopening the application. It seems that should I close my app and reopen it, it's fine but after leaving it for a while the title won't be there when I reopen it.

I have absolutely no idea what could be causing this so any help is appreciated. The layout of my application (relevant to this issue) is a basic splash screen (as an activity) with a loading bar which then opens the following FragmentActivity:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import static java.lang.Math.*;

public class FragmentControl extends SherlockFragmentActivity {

    private static final int NUM_PAGES = 3;

    private ViewPager mPager;

    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_control);

        ActionBar action = getSupportActionBar();
        action.setDisplayShowTitleEnabled(true);
        action.setDisplayShowHomeEnabled(false);

        mPager = (ViewPager)findViewById(R.id.pager);
        mPagerAdapter = new FragmentControlAdapter(getSupportFragmentManager(), NUM_PAGES);
        mPager.setAdapter(mPagerAdapter);
        // If this activity wasn't called after a reload
        if((Integer)getIntent().getExtras().get("current") == null){
            // Always start on the middle page, or as close as possible
            mPager.setCurrentItem((int) ceil(NUM_PAGES/2));
        // Otherwise start on the page we left for a smoother experience
        } else {
            mPager.setCurrentItem((Integer)getIntent().getExtras().get("current"));
        }
    }
}

Only when the application re-opens to the FragmentActivity do I see this issue, when reopening on anything else and navigating to this activity it's fine (as you'd expect).

Any and all help is appreciated, hope I've made it clear.

Oh, and if it matters I'm currently targeting API 17 with minimum support for API 8. The test phone I'm seeing this issue on is a HTC One S - not sure on other devices yet but I'm going to start looking.


Solution

  • occasionally (...) after leaving it for a while

    This sounds like your application process is killed in the meantime.

    Make sure to save instance state (like what title is displayed) using onSaveInstanceState and restore it in Activity.onCreate or Fragment.onViewCreated.