使用BottomNavigationView在片段之间进行切换时保留片段状态

时间:2018-09-05 15:44:16

标签: java android android-fragments view

我有一个Bottom Navigation View,我想以这样的方式使用fragmentNavigation:每次用户单击navigation视图的部分时, fragment应该加载先前的状态,而不是创建新的fragment。我有一个Frame Layout,所有fragment应该在该Frame Layout中加载或替换。 这是我的代码opublic类MainActivity扩展了AppCompatActivity {

    BottomNavigationView navigation;
    Home home;
    Trending trending;
    Upload upload;
    ImageView uploadImage;
    final public static int UPLOAD_ACTIVITY_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = findViewById(R.id.navigation);
        uploadImage = findViewById(R.id.uploadimg);
        home = new Home();
        trending = new Trending();
        upload = new Upload();
        fragmentLoad(home);
        uploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
                startActivityForResult(intent,UPLOAD_ACTIVITY_CODE);
            }
        });
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        fragmentLoad(home);
                        break;
                    case R.id.trending:
                        fragmentLoad(trending);
                        break;
                    case R.id.upload:
                        fragmentLoad(upload);
                        break;
                }
                return true;
            }
        });
    }

    public void fragmentLoad(android.support.v4.app.Fragment fragment){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame, fragment);
        transaction.commit();
    }

    public void fragmentLoad(android.support.v4.app.Fragment fragment,Bundle bundle){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().show(fragment);
        fragment.setArguments(bundle);
        transaction.replace(R.id.frame, fragment);
        transaction.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==UPLOAD_ACTIVITY_CODE && resultCode==UPLOAD_ACTIVITY_CODE && data!=null){
            String desc = data.getStringExtra("Desc");
            String title= data.getStringExtra("Title");
            String photo= data.getStringExtra("PhotoUri");
            String duration= data.getStringExtra("duration");
            String video= data.getStringExtra("videoUri");
            Bundle bundle = new Bundle();
            bundle.putString("desc",desc);
            bundle.putString("title",title);
            bundle.putString("photo",photo);
            bundle.putString("duration",duration);
            bundle.putString("videoUri",video);
            fragmentLoad(upload,bundle);
            navigation.setSelectedItemId(R.id.upload);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您应该从每个片段创建一个实例,然后使用FragmentManager根据底部导航状态显示它们。您应该在setData片段类中编写一个名为Upload的方法来重新初始化其视图。我为这种情况写了一个课程,希望对您有帮助。

public class MainActivity extends AppCompatActivity  {

    public static final int UPLOAD_ACTIVITY_CODE = 100;
    private BottomNavigationView navigation;
    private ImageView uploadImage;
    private Home home;
    private Trending trending;
    private Upload upload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = findViewById(R.id.navigation);
        uploadImage = findViewById(R.id.uploadimg);
        home = new Home();
        trending = new Trending();
        upload = new Upload();

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.getMenu().findItem(R.id.home).setChecked(true);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame, home)
                .add(R.id.frame, category)
                .add(R.id.frame, search)
                .commit();
        setTabStateFragment(TabState.HOME);

        uploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
                startActivityForResult(intent, UPLOAD_ACTIVITY_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UPLOAD_ACTIVITY_CODE && resultCode == UPLOAD_ACTIVITY_CODE && data != null) {
            String desc = data.getStringExtra("Desc");
            String title = data.getStringExtra("Title");
            String photo = data.getStringExtra("PhotoUri");
            String duration = data.getStringExtra("duration");
            String video = data.getStringExtra("videoUri");
            Bundle bundle = new Bundle();
            bundle.putString("desc", desc);
            bundle.putString("title", title);
            bundle.putString("photo", photo);
            bundle.putString("duration", duration);
            bundle.putString("videoUri", video);
            upload.setData(bundle);
            setTabStateFragment(TabState.UPLOAD);
        }
    }

    private void setTabStateFragment(TabState state) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (state) {
            case HOME: {
                transaction.show(home);
                transaction.hide(trending);
                transaction.hide(upload);
            }
            case TRENDING: {
                transaction.hide(home);
                transaction.show(trending);
                transaction.hide(upload);
            }
            case UPLOAD: {
                transaction.hide(home);
                transaction.hide(trending);
                transaction.show(upload);
            }
        }
        transaction.commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home: {
                    setTabStateFragment(TabState.HOME);
                    return true;
                }
                case R.id.trending: {
                    setTabStateFragment(TabState.TRENDING);
                    return true;
                }
                case R.id.upload: {
                    setTabStateFragment(TabState.UPLOAD);
                    return true;
                }
            }
            return false;
        }
    };

    enum TabState {
        HOME,
        TRENDING,
        UPLOAD,
    }

}