IBlackVikingl
IBlackVikingl

Reputation: 1233

How to display fragment at activity start up

I have an activity with button's. On press they display different fragment. But on activity start, FrameLayout(which used to display fragments) is empty/blank. How can i make it to display news_ru fragment at activity startup?

Here is my code for fragment display:

        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new news_ru());
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new news_kz());
            }
        });

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.newsFL, fragment);
        fragmentTransaction.commit();
    }

Upvotes: 0

Views: 43

Answers (1)

M. Bilal Asif
M. Bilal Asif

Reputation: 715

Call your private void replaceFragment(Fragment fragment) method inside onCreate of Activity after initialzing new xyzFragment() in your case it would be like

replaceFragment(new news_ru());

Upvotes: 1

Related Questions