2

Background Info: I currently have an AppCompatActivity which is working. But right now, everything is in the onCreate method. I am concerned it is causing some little bugs I've been having lately with my PagerAdapter having java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! The bugs only happen when I switch activities back and forth very fast. I wonder if fixing this will fix the bug.

System requirements: Min SDK: 15, Target SDK: 23 (at the time of this writing, or whatever is most current)

Question: How do I move my user interface initialization code into the onCreateView method?

I am familiar with how to implement the onCreate method of android.support.v4.app.Fragment, like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_songdetail, container, false);
    rootView.findViewById(R.id.info).setVisibility(View.INVISIBLE);

    return rootView;
}

But there are no options like that for AppCompatActivity. These are my options, from the AppCompatActivity docs:

View onCreateView(String name, Context context, AttributeSet attrs)

View onCreateView(View parent, String name, Context context, AttributeSet attrs)

Here is my code: (I omitted the methods besides onCreate and onCreateView)

public class DealPage extends AppCompatActivity
{
    private Deal deal;
    private Poll poll;
    private Video video;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        RealmDatabase.setRealmInstance(this);

        //----------- UNPACK EXTRAS -----------
        String date = getIntent().getExtras().getString(KeyStrings.EXTRA_DATE);

        //---------------- PREREQUISITE INITIALIZATION ----------
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_with_spinner);
        setSupportActionBar(toolbar);

        //------------ Enable "UP" navigation ---------------
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getDataFromDatabase(date);

        //----------------- THEMES AND COLORS ------------------

        //Set up colors
        final int backgroundColor = this.deal.getTheme().getBackgroundColor().getColor();
        final int accentColor = this.deal.getTheme().getAccentColor().getColor();
        String themeForeground = this.deal.getTheme().getForeground();
        final int foreground = generateForegroundColor(themeForeground);
        final String foregroundWebView = generateForegroundWebViewString(themeForeground); // This is necessary because HTML does not have an alpha channel.

        //Set toolbar colors
        toolbar.setBackgroundColor(accentColor);
        toolbar.setTitleTextColor(backgroundColor);

        //Set Page Background Color
        RelativeLayout dealPageBackground = (RelativeLayout) findViewById(R.id.deal_page_background);
        dealPageBackground.setBackgroundColor(backgroundColor);

        //----------- INITIALIZE THE ACTUAL DEAL PAGE STUFF ----------------

        //Title
        TextView title = (TextView) findViewById(R.id.title);
        title.setText(this.deal.getTitle());
        title.setTextColor(foreground);

        //Price
        TextView price = (TextView) findViewById(R.id.price);
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        price.setText(fmt.format(this.deal.getItems().first().getPrice()));
        price.setTextColor(foreground);

        //ViewInBrowser
        setUpViewInBrowserButton(backgroundColor, accentColor);

        AndDown andDown = new AndDown();

        //Set up "linkColorHTML"
        String linkColorHTML = generateLinkColorHTML(accentColor);

        //Features
        setUpFeaturesView(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //More Specs button
        setUpMoreSpecsButton(backgroundColor, foreground, (Spinner) findViewById(R.id.spinner));

        //Story Title
        TextView storyTitle = (TextView) findViewById(R.id.story_title);
        storyTitle.setText(this.deal.getStory().getTitle());
        storyTitle.setTextColor(accentColor);

        //Story Body
        setUpStoryBody(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //Specs Title
        TextView specsTitle = (TextView) findViewById(R.id.specs_title);
        specsTitle.setText(this.deal.getTitle());
        specsTitle.setTextColor(accentColor);

        //Specs
        setUpSpecificationsView(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //Set up ViewPager
        ViewPager viewPager = (ViewPager) findViewById(R.id.photos_view_pager);
        viewPager.setAdapter(new PhotoPagerAdapter(this, this.deal.getPhotos()));

        // Setup spinner
        setUpSpinner(accentColor, backgroundColor, toolbar);
    }


    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs)
    {
        return super.onCreateView(name, context, attrs);
    }

}
Rock Lee
  • 9,146
  • 10
  • 55
  • 88

1 Answers1

0

You must use a PlaceholderFragment. Try this:

public class RestaurantListActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment(), "fragmentName").commit();
    }
}

public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {...}

alvaro torrico
  • 675
  • 6
  • 17