配置应用程序启动时首先显示的片段

时间:2017-07-29 03:03:54

标签: android android-fragments

我已经创建了一个标签式活动,因此我已经做了很多事情。我跟进了一个教程,当点击抽屉项目时,更改为另一个片段。现在,在初始化我的应用时,我需要将MainFragment设为默认值。我实际上不知道现在出现了什么。

下面是负责处理片段更改的MainActivity代码:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_home:
                setFragmentTransaction(new MainFragment(), "Dashboard");
                break;
            case R.id.nav_accounts:
                setFragmentTransaction(new AccountFragment(), "Accounts");
                break;
            case R.id.nav_categories:
                break;
            case R.id.nav_transactions:
                break;
            case R.id.nav_reports:
                break;
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

       return true;
    }

    private void setFragmentTransaction(Fragment fragment, String title) {
        setTitle(title);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}

我尝试设置default,但它没有影响任何内容。

2 个答案:

答案 0 :(得分:2)

setFragmentTransaction(new MainFragment(), "Dashboard");添加到onCreate的底部。这应该会加载你的主片段。

答案 1 :(得分:1)

你可以这样做,你必须在onCreate本身加载你的一个片段,以便它可以在应用程序启动时启动。

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.nav_view)
    NavigationView navigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;

    // tags used to attach the fragments
    public static final String TAG_CHAR = "characters";
    public static final String TAG_CO = "comics";
    public static final String TAG_EV = "events";
    public static final String TAG_AB = "about";
    public static String CURRENT_TAG = TAG_CHAR;

    // index to identify current nav menu item
    public static int navItemIndex = 0;

    public Fragment fragment;

    private Fragment sendFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);

        if (savedInstanceState != null) {
            fragment = getSupportFragmentManager().getFragment(savedInstanceState, "myFragmentName");
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commit();
        } else {
            navItemIndex = 0;
            CURRENT_TAG = TAG_CHAR;
            loadHomeFragment();
        }

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
    }

    private void loadHomeFragment() {
        selectNavMenu();

        if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
            drawer.closeDrawers();
            return;
        } else {
            fragment = getHomeFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commit();
            drawer.closeDrawers();
        }
    }

    private Fragment getHomeFragment() {
        switch (navItemIndex) {
            case 0:
                sendFragment = new CharacterFragment();
                break;
            case 1:
                sendFragment = new ComicFragment();
                break;
            case 2:
                sendFragment = new EventFragment();
                break;
            case 3:
                sendFragment = new AboutFragment();
                break;
            default:
                sendFragment = new CharacterFragment();
        }
        return sendFragment;
    }

    private void selectNavMenu() {
        navigationView.getMenu().getItem(navItemIndex).setChecked(true);
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
            return;
        }
        super.onBackPressed();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {

        switch (menuItem.getItemId()) {
            case R.id.nav_char:
                navItemIndex = 0;
                CURRENT_TAG = TAG_CHAR;
                break;
            case R.id.nav_com:
                navItemIndex = 1;
                CURRENT_TAG = TAG_CO;
                break;
            case R.id.nav_ev:
                navItemIndex = 2;
                CURRENT_TAG = TAG_EV;
                break;
            case R.id.nav_about_us:
                navItemIndex = 3;
                CURRENT_TAG = TAG_AB;
                break;
            default:
                navItemIndex = 0;
        }
        if (menuItem.isChecked()) {
            menuItem.setChecked(false);
        } else {
            menuItem.setChecked(true);
        }
        menuItem.setChecked(true);

        loadHomeFragment();
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "myFragmentName", fragment);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onRestoreInstanceState(savedInstanceState, persistentState);
    }
}